[R] Merge data frame and keep unmatched
Marc Schwartz
marc_schwartz at me.com
Wed Jun 10 16:02:28 CEST 2009
On Jun 10, 2009, at 8:56 AM, Etienne B. Racine wrote:
>
> Hi,
>
> With two data sets, one complete and another one partial, I would
> like to
> merge them and keep the unmatched lines. The problem is that merge()
> dosen't
> keep the unmatched lines. Is there another function that I could use
> to
> merge the data frames.
>
> Example:
>
> completedf <- expand.grid(alpha=letters[1:3],beta=1:3)
> partdf <- data.frame(
> alpha= c('a','a','c'),
> beta = c(1,3,2),
> val = c(2,6,4))
>
> mergedf <- merge(x=completedf, y=partdf, by=c('alpha','beta'))
> # it only kept the common rows
> nrow(mergedf)
>
> Thanks,
> Etienne
Is this what you want?
> merge(x=completedf, y=partdf, by=c('alpha','beta'), all = TRUE)
alpha beta val
1 a 1 2
2 a 2 NA
3 a 3 6
4 b 1 NA
5 b 2 NA
6 b 3 NA
7 c 1 NA
8 c 2 4
9 c 3 NA
Note the 'all', 'all.x' and 'all.y' arguments...
HTH,
Marc Schwartz
More information about the R-help
mailing list