I want to add individual elements of a char vector as columns to a list of data.frames. I can do it manually, but is there a more elegant lapply-way?
# Create sample dfs
set.seed(1)
df1 <- data.frame("one" = sample(1:10,10,replace=TRUE),
"two" = sample(1:10,10,replace=TRUE))
df2 <- data.frame("three" = sample(1:10,10,replace=TRUE),
"four" = sample(1:10,10,replace=TRUE))
df3 <- data.frame("five" = sample(1:10,10,replace=TRUE),
"six" = sample(1:10,10,replace=TRUE))
# Combine them to list
dflist = list("df1"=df1,"df2"=df2,"df3"=df3)
# add labelling column
dflist$df1["label"] <- "a"
dflist$df2["label"] <- "b"
dflist$df3["label"] <- "c"
# With lapply I can only add either "a", "b" or "c"
dflist = list("df1"=df1,"df2"=df2,"df3"=df3)
labvec <- c("a","b","c")
lapply(dflist,function(x) cbind(x,labvec[2])) # I have to select 1, 2 or 3.
Asked differently: Could i also index over "labvec" with lapply?