[R] Combining CSV data

arun smartpink111 at yahoo.com
Tue Jun 11 00:41:58 CEST 2013

Try this:

Row_ID_CR,  Data1,    Data2,    Data3
1,                  aa,          bb,          cc
2,                  dd,          ee,          ff

Row_ID_N,  Src_Row_ID,  DataN1
1a,              1,                  This is comment 1
2a,              1,                  This is comment 2
3a,              2,                  This is comment 1
4a,              1,                  This is comment 3
res<- merge(dat1,dat2,by.x=1,by.y=2)
 res2<-ddply(res1,.(Row_ID_CR,Data1,Data2,Data3),summarize, DataN1=list(DataN1))
 # Row_ID_CR                Data1        Data2        Data3
#1         1                   aa           bb           cc
#2         2                   dd           ee           ff
#                                                   DataN1
#1 This is comment 1, This is comment 2, This is comment 3
#2                                       This is comment 1

res3<-data.frame(res2[,-5],t(apply(do.call(rbind,res2[,5]),1,function(x) {x[duplicated(x)]<-NA;x})))
 colnames(res3)[grep("X",colnames(res3))]<- paste0("DataComment",gsub("[[:alpha:]]","",colnames(res3)[grep("X",colnames(res3))]))
#  Row_ID_CR                Data1        Data2        Data3      DataComment1
#1         1                   aa           bb           cc This is comment 1
#2         2                   dd           ee           ff This is comment 1
#       DataComment2      DataComment3
#1 This is comment 2 This is comment 3
#2              <NA>              <NA>


----- Original Message -----
From: Shreya Rawal <rawal.shreya at gmail.com>
To: r-help at r-project.org
Sent: Monday, June 10, 2013 4:38 PM
Subject: [R] Combining CSV data

Hello R community,

I am trying to combine two CSV files that look like this:

File A

Row_ID_CR,   Data1,    Data2,    Data3
1,                   aa,          bb,          cc
2,                   dd,          ee,          ff

File B

Row_ID_N,   Src_Row_ID,   DataN1
1a,               1,                   This is comment 1
2a,               1,                   This is comment 2
3a,               2,                   This is comment 1
4a,               1,                   This is comment 3

And the output I am looking for is, comparing the values of Row_ID_CR and


ROW_ID_CR,    Data1,    Data2,    Data3,    DataComment1,
DataComment2,          DataComment3
1,                      aa,         bb,         cc,        This is
comment1,    This is comment2,     This is comment 3
2,                      dd,          ee,         ff,          This is

I am a novice R user, I am able to replicate a left join but I need a bit
more in the final result.


    [[alternative HTML version deleted]]

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list