Hi I am trying to add the contents of the list "myList" to a new column "z" in the data frame "myDataframe" myList <- list(c("A1","B1"), c("A2","B2","C2"), c("A3","B3")) myDataframe <- data.frame(x=c(1,2,3), y=c("R","S","T")) Would like to produce x y z 1 R A1,B1 2 S A2,B2,C2 3 T A3,B3 where z is a character string holding the contents of the different elements of the list. Any thoughts greatly appreciated. Pete -- View this message in context: http://r.789695.n4.nabble.com/Adding-List-Elements-To-A-Data-Frame-tp4671932.html Sent from the R help mailing list archive at Nabble.com.
On 7/19/2013 12:54 PM, Pete Brecknock wrote:> Hi > > I am trying to add the contents of the list "myList" to a new column "z" in > the data frame "myDataframe" > > myList <- list(c("A1","B1"), c("A2","B2","C2"), c("A3","B3")) > > myDataframe <- data.frame(x=c(1,2,3), y=c("R","S","T")) > > Would like to produce > > x y z > 1 R A1,B1 > 2 S A2,B2,C2 > 3 T A3,B3myDataframe$z <- sapply(myList, paste0, collapse=",")> where z is a character string holding the contents of the different elements > of the list. > > Any thoughts greatly appreciated. > > Pete-- Brian S. Diggs, PhD Senior Research Associate, Department of Surgery Oregon Health & Science University
On Jul 19, 2013, at 12:54 PM, Pete Brecknock wrote:> Hi > > I am trying to add the contents of the list "myList" to a new column "z" in > the data frame "myDataframe" > > myList <- list(c("A1","B1"), c("A2","B2","C2"), c("A3","B3")) > > myDataframe <- data.frame(x=c(1,2,3), y=c("R","S","T")) > > Would like to produce > > x y z > 1 R A1,B1 > 2 S A2,B2,C2 > 3 T A3,B3 > > where z is a character string holding the contents of the different elements > of the list.> myDataframe[[3]] <- I(myList) > myDataframex y z 1 1 R A1, B1 2 2 S A2, B2, C2 3 3 T A3, B3 -- David Winsemius Alameda, CA, USA