I have two matrices in a list:
colList <- list()
colList[["V1"]] <- as.matrix(c("asd", "asd", "asd"))
colList[["V2"]] <- as.matrix(c("das", "das", "das"))
And I want to cbind the values of a data.frame value.frame$keyID
to each sublist. The first value (2000) to the first sublist, the second value (3000) to the second sublist.
Here the value.frame:
value.frame <- data.frame(keyID =c("2000", "3000"))
The result should look like this:
colList <- list()
colList[["V1"]] <- matrix(c("asd", "asd", "asd", 2000, 2000, 2000),
nrow=3,
ncol=2)
colList[["V2"]] <- matrix(c("das", "das", "das", 3000, 3000, 3000),
nrow=3,
ncol=2)
I tried it with the following code, but the result is not the desired one. Hope someone can help me.
mapply( cbind, colList, paste(value.frame[,1]))
Using lapply
and seq_along
nms <- names(colList)
colList <- lapply(seq_along(colList), x=colList,
y=as.character(value.frame$keyID), function(j, x, y) {
cbind(x[[j]], y[j])
})
names(colList) <- nms
colList[["V1"]]
[,1] [,2]
[1,] "asd" "2000"
[2,] "asd" "2000"
[3,] "asd" "2000"
colList[["V2"]]
[,1] [,2]
[1,] "das" "3000"
[2,] "das" "3000"
[3,] "das" "3000"