开发者

Merge Command in R

开发者 https://www.devze.com 2023-01-12 16:52 出处:网络
I have been playing around with th开发者_开发百科e merge command in R and am trying to figure out how to use the SUFFIX parameter. The online documentation does not do a very good job at explaining it

I have been playing around with th开发者_开发百科e merge command in R and am trying to figure out how to use the SUFFIX parameter. The online documentation does not do a very good job at explaining it.

What I'd like to do is import some csv files:

data1<-read.csv("fileA", header=T)
data2<-read.csv("fileB", header=T)

And then use the merge command to merge them. However, I would like for some variables to be truly merged, while other variables that hold the same name to be labeled by the file they came from. So for instance, if a "NAME" variable existed in both of my spreadsheets, then I'd like for them to be merged as normal, but if a "GRADE" variable showed up, it would be changed to GRADE.fileA and GRADE.fileB. I am already able to get GRADE.x and GRADE.y, but I would prefer more useful labels on these variables. Any help on this would be appreciated. Thank you.


I'm guessing that you didn't explicitly specify the by arg in merge. Do you want something like this?

> NAME <- sample(letters,10)
> data1 <- data.frame(NAME,grade=sample(letters[1:4],10,TRUE))
> data2 <- data.frame(NAME,grade=sample(letters[1:4],10,TRUE))
> merged <- merge(data1,data2,by="NAME",suffixes=c(".fileA",".fileB"))
> merged
   NAME grade.fileA grade.fileB
1     d           a           c
2     e           d           d
3     f           b           a
4     j           c           c
5     l           b           a
6     o           a           c
7     p           d           d
8     q           d           a
9     t           a           b
10    x           d           c


I think this should work:

merged.df <- merge(data1, data2, by='NAME', suffixes=c('.fileA', '.fileB'))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号