Reputation: 1848
Let I have two such data frames:
data frame1(df1):
col1 col2
x x
x x
x y
y y
data frame2(df2):
col1 col2
1.1 0.1
1.3 1.0
0.3 0.8
0.7 1.6
Desired output data frame(df) is:
col1 col2
0.7 0.8
NA 1.6
Namely, I want to produce a subset of df2, which mirrors df1="y". How can I do that using R? I will be very glad for any help? Thanks a lot.
Upvotes: 2
Views: 212
Reputation: 66819
You can iterate over columns:
lapply(1:ncol(df1),function(i) df2[[i]][df1[[i]]=="y"])
Or similarly:
mapply(function(a,b) a[b=="y"], df2,df1)
The result is a list
, not a data.frame
, but this seems best (since rows have no meaning).
Upvotes: 3