try this:

> fileA <- read.csv(text = "Row_ID_CR,   Data1,    Data2,    Data3
+ 1,                   aa,          bb,          cc
+ 2,                   dd,          ee,          ff", as.is = TRUE)
>
> fileB <- read.csv(text = "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", as.is = TRUE)
>
> # get rid of leading/trailing blanks on comments
> fileB$DataN1 <- gsub("^ *| *$", "", fileB$DataN1)
>
> # merge together
> result <- merge(fileA, fileB, by.x = 'Row_ID_CR', by.y = "Src_Row_ID")
>
> # now partition by Row_ID_CR and aggregate the comments
> result2 <- do.call(rbind,
+     lapply(split(result, result$Row_ID_CR), function(.grp){
+         cbind(.grp[1L, -c(5,6)], comment = paste(.grp$DataN1, collapse =
', '))
+     })
+ )
> result2
  Row_ID_CR                 Data1        Data2        Data3
                                comment
1         1                    aa           bb           cc This is comment
1, This is comment 2, This is comment 3
2         2                    dd           ee           ff
                      This is comment 1
>



On Mon, Jun 10, 2013 at 4:38 PM, Shreya Rawal <rawal.shr...@gmail.com>wrote:

> 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
> Src_Row_ID
>
> Output
>
> 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
> comment1
>
>
> I am a novice R user, I am able to replicate a left join but I need a bit
> more in the final result.
>
>
> Thanks!!
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to