如果列表有或没有 Headers ,似乎purrr函数的评估方式不同,但为什么呢?这只是“其中一件事”吗?
例:
func_b <- function(x,y,z) paste(x,y,z)
## Works as expected
pmap(list(iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length), func_b) %>% head(2)
[[1]] [1]“5.1 3.5 1.4”[[2]] [1]“4.9 3 1.4”
## Doesn't work
pmap(map(iris[,1:3],list),func_b)
.f中的错误(Sepal.Length = .l [[c(1L,1L)]],Sepal.Width = .l [[c(2L,1L)]],:未使用的参数(Sepal.Length = .l [[c(1,1)]],Sepal.Width = .1 [[c(2,1)]],Petal.Length = .1 [[c(3,1)]])
但唯一的区别似乎是其中一个名单保留了其头衔,而另一个则没有?
list(iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length) %>% map(head)
[[1]] [1] 5.1 4.9 4.7 4.6 5.0 5.4 [[2]] [1] 3.5 3.0 3.2 3.1 3.6 3.9 [[3]] [1] 1.4 1.4 1.3 1.5 1.4 1.7
as.list(iris[,1:3]) %>% map(head)
$ Sepal.Length [1] 5.1 4.9 4.7 4.6 5.0 5.4 $ Sepal.Width [1] 3.5 3.0 3.2 3.1 3.6 3.9 $ Petal.Length [1] 1.4 1.4 1.3 1.5 1.4 1.7
class(list(iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length)) == class(as.list(iris[,1:3]))
[1]是的
str(list(iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length)); str(as.list(iris[,1:3]))
List of 3
$ : num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ : num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
List of 3
$ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
如果我们只是删除第二个列表的名称,它的工作没有问题 .
aa <- as.list(iris[,1:3])
names(aa) <- NULL
pmap(aa,func_b) %>% head(2)
[[1]] [1]“5.1 3.5 1.4”[[2]] [1]“4.9 3 1.4”
所以我的具体问题是:为什么 Headers 会影响评估方法?有没有办法转换东西而不破坏dplyr管道来消除名称?
1 回答
Axeman是现货,但你可以防御和动态编程: