Re: [R] subset rows in two dataframes

From: jim holtman <jholtman_at_gmail.com>
Date: Sat, 10 May 2008 18:42:51 -0400

This seems to work for me:

> set.seed(1)
> df1 <- data.frame(v1=factor(sample(1:4,20,TRUE)), v2=factor(sample(1:3,20,TRUE)), v3=sample(1:3,20,TRUE))
> df2 <- data.frame(v1=factor(sample(1:2,20,TRUE)), v2=factor(sample(1:2,20,TRUE)), v3=sample(1:2,20,TRUE))
> subset(df1, (df1$v1 %in% df2$v1) & (df1$v2 %in% df2$v2) & (df1$v3 %in% df2$v3))

   v1 v2 v3
2 2 1 2
5 1 1 2
11 1 2 2
14 2 1 1
>

Exactly what problems are you having? A sample of your actual data would be useful.

On Sat, May 10, 2008 at 6:31 PM, <partofy_at_inoutbox.com> wrote:
> Dear list:
>
> I have two dataframes, say dat1 and dat2. Each has several variables but
> 3 of each are common in both, (say v1, v2 and v3). v1 and v2 are
> factores while v3 is numeric. Now, I need a subset to extract the rows
> in which v1, v2 and v3 are the same in both dataframes.
> I tried:
>
> subset(dat1, dat1$v1 %in% dat2$v1 & dat1$v2 %in% dat2$v2 & dat1$v3 %in%
> dat2$v3)
>
> I dont know why, but this is not working as I was expecting. Any
> suggestion to improve my code?
>
> Thanks in advance
>
> Justin
> --
>
> partofy_at_inoutbox.com
>
> --
>
> ______________________________________________
> R-help_at_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.
>

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
R-help_at_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.
Received on Sat 10 May 2008 - 22:45:11 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 11 May 2008 - 14:30:36 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive