R-`purrr :: map()`中的复合函数与管道函数 [英] R - Composite functions vs piped functions in `purrr::map()`

查看:90
本文介绍了R-`purrr :: map()`中的复合函数与管道函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下列表:

my_list = list(alpha = list('hi'), 
               beta = list(1:4, 'how'), 
               gamma = list(TRUE, FALSE, 'are'))
str(my_list)

List of 3
 $ alpha:List of 1
  ..$ : chr "hi"
 $ beta :List of 2
  ..$ : int [1:4] 1 2 3 4
  ..$ : chr "how"
 $ gamma:List of 3
  ..$ : logi TRUE
  ..$ : logi FALSE
  ..$ : chr "are"

我想弄清楚每个1级元素中包含哪些数据类型。为此,我可以使用以下管道:

I would like to figure out which data types are contained within each level 1 element. To do this, I can use the following pipeline:

piped = map(my_list, ~map(., class) %>% unique %>% unlist)
str(piped)
List of 3
 $ alpha: chr "character"
 $ beta : chr [1:2] "integer" "character"
 $ gamma: chr [1:2] "logical" "character"

...按预期工作。但是,当我尝试将对 unique 的调用嵌套在 unlist()内时,我得到了一些不同的东西:

...which works as expected. But when I try to nest the call to unique inside unlist(), I get something different:

composite = map(my_list, ~map(., class) %>% unlist(unique(.)))
str(composite)
List of 3
 $ alpha: chr "character"
 $ beta : chr [1:2] "integer" "character"
 $ gamma: chr [1:3] "logical" "logical" "character"

有人可以帮我理解为什么这两个原因吗?

Could someone please help me understand why these two approaches are not equivalent?

推荐答案

如果您使用匿名函数而不是点表示法,则更容易看到正在发生的事情。

It's easier to see what's happening if you use anonymous functions rather than the dot notation.

piped = map(my_list,〜map(。,class)%&%;%unique%>%unlist)

piped = map(my_list,function(x)map(x,class)%&%;%unique%>%unlist )

然后管道将每个步骤的输出放置在每个后续函数的第一位置,因此它成为

The pipe then places the output of each step in the first position of each subsequent function, so it becomes

piped = map(my_list,function(x)unique(map(x,class))%>%unlist)然后

piped = map(my_list,function(x)unlist(unique(map(x(class,)))))

结果是

str(piped)
List of 3
 $ alpha: chr "character"
 $ beta : chr [1:2] "integer" "character"
 $ gamma: chr [1:2] "logical" "character"

类似地 composite = map(my_list,〜map(。,class)%>%unlist(唯一(。)))

可以写为

composite = map(my_list,function(x)map(x,class)%>%unlist(unique(x)))

在管道两边使用x的两种用法使事情变得很奇怪。我认为您期望管道将内部地图调用的输出放置在 unique 调用中的x处,但是第二个x从外部地图获取输出功能。相反,管道将执行其默认操作,并将内部映射调用的输出放置在 unlist 函数的第一位置,就像这样

The two uses of x on both sides of the pipe is where things go weird. I think you were expecting the pipe to place the output of the inner map call in place of the x within the unique call, but the second x takes the output from the outer map function. Instead the pipe does its default action and places the output of the inner map call in the first position of the unlist function, like so

composite = map(my_list,function(x)unlist(map(x,class),unique(x)))

哪个给你

str(composite)
List of 3
 $ alpha: chr "character"
 $ beta : chr [1:2] "integer" "character"
 $ gamma: chr [1:3] "logical" "logical" "character"

这篇关于R-`purrr :: map()`中的复合函数与管道函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆