使用带有mget的Map会产生意外的输出(Using Map with mget yields unexpected output)

编程入门 行业动态 更新时间:2024-10-28 14:29:39
使用带有mget的Map会产生意外的输出(Using Map with mget yields unexpected output)

我有很多列表:比如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,10

I 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)))

更多推荐

本文发布于:2023-07-08 12:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1076414.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:意外   Map   mget   output   unexpected

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!