我有很多列表:比如list1,list2,....和list 100以及myfun函数。 有了这些,我可以运行Map(myfun,list1,list2,...,list100) 。 但是,我想避免编写所有列表编号,所以我尝试了Map(myfun,mget(paste0("list",1:100))) 。 但是,这没有用。 下面提供了一个只有三个列表的可重现示例:
x1<-list(1:10,10:20) x2<-list(0:9,11:20) x3<-list(100:109,41:50) Map(rbind,x1,x2,x3) #ok mget(paste0("x",1:3)) #ok Map(rbind,mget(paste0("x",1:3))) #ok but the unexpected output Map(rbind,mget(paste0("x",1:3))) $x1 [,1] [,2] [1,] Integer,10 Integer,10 $x2 [,1] [,2] [1,] Integer,10 Integer,10 $x3 [,1] [,2] [1,] Integer,10 Integer,10I have a number of lists :say list1, list2,....and list 100 and function myfun . With these, I can run Map(myfun,list1,list2,...,list100). However, I want to avoid writing all list numbers, and so I tried Map(myfun,mget(paste0("list",1:100))). But, this didn't work. A reproducible example with only three lists is provided below:
x1<-list(1:10,10:20) x2<-list(0:9,11:20) x3<-list(100:109,41:50) Map(rbind,x1,x2,x3) #ok mget(paste0("x",1:3)) #ok Map(rbind,mget(paste0("x",1:3))) #ok but the unexpected output Map(rbind,mget(paste0("x",1:3))) $x1 [,1] [,2] [1,] Integer,10 Integer,10 $x2 [,1] [,2] [1,] Integer,10 Integer,10 $x3 [,1] [,2] [1,] Integer,10 Integer,10最满意答案
mget返回列表列表(参见“值”下的?mget ),而不是将每个列表解析为Map的单独向量。 处理它的一种可能方法是将列表rbind为原始元素,然后对其进行处理,例如
Reduce(function(...) Map(rbind, ...), mget(paste0("x", 1:3)))mget returns a list of list (see ?mget under "Value"), as opposed to parsing each list as a separate vector into Map. A possible way to deal with it, is to Reduce the list back to its original elements and then rbind them, something like
Reduce(function(...) Map(rbind, ...), mget(paste0("x", 1:3)))更多推荐
发布评论