我在列表中有两个矩阵:
colList <- list()
colList[["V1"]] <- as.matrix(c("asd", "asd", "asd"))
colList[["V2"]] <- as.matrix(c("das", "das", "das"))
我想将data.frame value.frame$keyID
的值绑定到每个子列表。第一个子列表的第一个值(2000),第二个子列表的第二个值(3000)。
这里是value.frame:
value.frame <- data.frame(keyID =c("2000", "3000"))
结果应如下所示:
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)
我尝试使用以下代码,但结果不是所需的。希望可以有人帮帮我。
mapply( cbind, colList, paste(value.frame[,1]))
答案 0 :(得分:2)
使用lapply
和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"
答案 1 :(得分:1)
您可以使用mapply
SIMPLIFY=FALSE
执行此操作
mapply(cbind, colList, as.character(value.frame$keyID), SIMPLIFY=FALSE)
#$V1
# [,1] [,2]
#[1,] "asd" "2000"
#[2,] "asd" "2000"
#[3,] "asd" "2000"
#$V2
# [,1] [,2]
#[1,] "das" "3000"
#[2,] "das" "3000"
#[3,] "das" "3000"
或使用Map
作为mapply(..., SIMPLIFY=FALSE)
Map(cbind, colList, as.character(value.frame$keyID))