Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 运行基于向量元素的循环_R_Loops_Merge - Fatal编程技术网

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”是一个“每次”解决方案。