在 R 中使用 map() 函数内的管道操作符

9

在尝试在map()内部使用管道时,我得到了意外的结果。

map(ls(), ~ . %>% get %>% dim)

返回以下消息:

Functional sequence with the following components:

 1. get(.)
 2. dim(.)

Use 'functions' to extract the individual functions. 

我不太清楚如何使用函数()来得到我想要的结果。

有没有一种使用管道和map的方法来完成它呢?

如果不使用管道,

map(ls(), ~ get(dim(.)))

结果符合我的预期。


1
我会选择 lapply(mget(ls()), dim)(不使用管道)。 - talat
1个回答

12

. %>% get %>% dim 已经是一个函数,所以只需省略 ~ 即可。

map(ls(), . %>% get %>% dim)
或:
ls() %>% map(. %>% get %>% dim)

谢谢,那个可行!但是如果我用数据开始这个链,我并不真正理解它是如何成为一个函数的。 - vuzun
1
一个左手边是点的 magrittr 管道定义了一个函数。请参阅 help("%>%", "magrittr") 中的段落 使用点占位符作为 lhs - G. Grothendieck

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接