Re: [R] Merge data frame and keep unmatched

2009-07-13 Thread Matthew Dowle
Or if you need it to be fast,  try data.table.   X[Y] is a join when X and Y 
are both data.tables. X[Y] is a left join, Y[X] is a right join. 'nomatch' 
controls the inner/outer join i.e. what happens for unmatched rows.   This 
is much faster than merge().

"Gabor Grothendieck"  wrote in message 
news:971536df0906100704q433f5f99ld3f9c23e69d95...@mail.gmail.com...
Try:

merge(completedf, partdf, all.x = TRUE)

or

library(sqldf) # see http://sqldf.googlecode.com
sqldf("select * from completedf left join partdf using(beta, alpha)")


On Wed, Jun 10, 2009 at 9: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
> --
> View this message in context: 
> http://www.nabble.com/Merge-data-frame-and-keep-unmatched-tp23962874p23962874.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Merge data frame and keep unmatched

2009-06-10 Thread Gabor Grothendieck
Try:

merge(completedf, partdf, all.x = TRUE)

or

library(sqldf) # see http://sqldf.googlecode.com
sqldf("select * from completedf left join partdf using(beta, alpha)")


On Wed, Jun 10, 2009 at 9: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
> --
> View this message in context: 
> http://www.nabble.com/Merge-data-frame-and-keep-unmatched-tp23962874p23962874.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Merge data frame and keep unmatched

2009-06-10 Thread Marc Schwartz


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 a1   2
2 a2  NA
3 a3   6
4 b1  NA
5 b2  NA
6 b3  NA
7 c1  NA
8 c2   4
9 c3  NA

Note the 'all', 'all.x' and 'all.y' arguments...

HTH,

Marc Schwartz

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Merge data frame and keep unmatched

2009-06-10 Thread Etienne B. Racine

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
-- 
View this message in context: 
http://www.nabble.com/Merge-data-frame-and-keep-unmatched-tp23962874p23962874.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.