tags:

views:

70

answers:

2
+2  Q: 

Merge Command in R

Hello,

I have been playing around with the 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.

+2  A: 

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
Joshua Ulrich
+1  A: 

I think this should work:

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