Lets say I have big DataFrame and smaller one, which is its subset. Big one has column Y, and smaller has not, both have colmun ID. How, based on equality of values in ID assign values from Y in Big DF to small DF?
Asked
Active
Viewed 80 times
0
-
See `?merge` and try searching for `[r] merge` on this site - something like `merge(small,big,by="ID",all.x=TRUE)`. I suggest reading a good intro to R site like http://statmethods.net/ to get these sorts of issues sorted. – thelatemail May 05 '14 at 05:52
-
@thelatemail I dont want to merge them, I want that number of rows in resulting DF would be as small DF has. It must be small DF just with new column. – Gill Bates May 05 '14 at 11:50
-
1`merge` can give you what you want. `merge(small,big[c("ID","Y")],by="ID",all.x=TRUE)` if you only want the `Y` column added. It will return multiple values if there are multiple matches in `big`, but you probably want to know that you don't have a nice 1:1 relationship in that case. – thelatemail May 05 '14 at 20:42
1 Answers
0
If these are your data frames
BIG<-data.frame(ID=1:20, Y=runif(20))
SMALL<-data.frame(ID=c(3,7,12))
Then you can do the matching using match()
. For example
SMALL$Y<-BIG$Y[match(SMALL$ID, BIG$ID)]

MrFlick
- 195,160
- 17
- 277
- 295
-
-
-
I won't bother - this is a duplicate question, and I have voted to close rather than answering. – thelatemail May 05 '14 at 06:01