R 运行基于向量元素的循环
我必须运行一个基于特定向量值的循环。一些示例代码和数据如下所示:R 运行基于向量元素的循环,r,loops,merge,R,Loops,Merge,我必须运行一个基于特定向量值的循环。一些示例代码和数据如下所示: list_store <- list() vec <- c(3,2,3) data_list <- lapply(list(head(mtcars,10), head(mtcars,15), head(mtcars,20), head(mtcars, 9), head(mtcars,14), head(mtcars,18), head(mtcars,20), he
list_store <- list()
vec <- c(3,2,3)
data_list <- lapply(list(head(mtcars,10), head(mtcars,15), head(mtcars,20), head(mtcars, 9),
head(mtcars,14), head(mtcars,18), head(mtcars,20), head(mtcars,10)),
function(x) rownames_to_column(x))
data_list1 <- lapply(list(head(mtcars,7), head(mtcars,8), head(mtcars,10)), function(x) rownames_to_column(x))
result <- lapply(data_list, function(i){
list_store[[length(list_store) + 1]] <- merge(i, data_list1[[1]], all.y = TRUE)
})
list\u store我们按照“vec”的顺序复制“vec”,使用它将“data\u list”拆分为3个list
元素,每个元素都有一个list
。然后,使用Map
传递split
数据集和'data\u list1'中相应的list
元素,使用lappy
循环通过嵌套的list
和merge
与'data\u list1'元素,使用c
将嵌套的列表
转换回“数据列表”的正常列表
结构
do.call(c,
Map(function(x,y) lapply(x, function(dat)
merge(dat, y, all.y = TRUE)),
split(data_list, rep(seq_along(vec), vec)),
data_list1))
我可以说R
legendary@akrun:)@AaghazHussain谢谢你的大话,我仍然是一个谦逊的学习者。@AaghazHussain你是否用c
创建了一个对象,即c@AaghazHussain可能是某些对象的后遗症。直接在干净的桌面上试一试。因此,如果无意中创建了一个带有c的对象,“c”是一个“每次”解决方案。