Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

r - the rules of subsetting

Having df1 and df2 as follows:

df1 <- read.table(text =" x y z
                          1 1 1
                          1 2 1
                          1 1 2
                          2 1 1
                          2 2 2",header=TRUE)

df2 <- read.table(text =" a b c
                          1 1 1
                          1 2 8
                          1 1 2
                          2 6 2",header=TRUE)

I can ask of the data a bunch of things like:

 df2[ df2$b == 6 | df2$c == 8 ,] #any rows where b=6 plus c=8 in df2
 #and additive conditions
 df2[ df2$b == 6 & df2$c == 8 ,] # zero rows

between data.frame:

 df1[ df1$z %in% df2$c ,] # rows in df1 where values in z are in c (allrows)

This gives me all rows:

 df1[ (df1$x %in%  df2$a) &
      (df1$y %in%  df2$b) &
      (df1$z %in%  df2$c) ,]

but shouldn't this give me all rows of df1 too:

 df1[ df1$z %in% df2$c | df1$b == 9,]

What I am really hoping to do is to subset df1 an df2 on three column conditions, so that I only get rows in df1 where a,b,c all equal x,y,z at the same time within a row. In real data i will have more than 3 columns but I will still want to subset on 3 additive column conditions.

So subsetting my example data df1 on df2 my result would be:

df1
   1 1 1
   1 1 2

Playing with syntax has confusedme more and the SO posts are all variaion of what I want that actually lead to more confusion for me.

I figured out I can do this:

 merge(df1,df2, by.x=c("x","y","z"),by.y=c("a","b","c"))

which gives me what I want, but I would like to understand why I am wrong in my [ attempts.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In addition to your nice solution using merge (thanks for that, I always forget merge), this can be achieved in base using ?interaction as follows. There may be other variations of this, but this is the one I am familiar with:

> df1[interaction(df1) %in% interaction(df2), ]

Now to answer your question: First, I think there's a typo (corrected) in:

df1[ df1$z %in% df2$c | df2$b == 9,] # second part should be df2$b == 9

You would get an error, because the first part evaluates to

[1] TRUE TRUE TRUE TRUE TRUE

and the second evaluates to:

[1] FALSE FALSE FALSE FALSE

You do a | operation on unequal lengths getting the error:

longer object length is not a multiple of shorter object length

Edit: If you have multiple columns then you can choose the interaction as such. For example, if you want to get from df1 the rows where the first two columns match with that of df2, then you could simply do:

> df1[interaction(df1[, 1:2]) %in% interaction(df2[, 1:2]), ]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...