purrr表评价奇异性
如果列表有或没有标题,purrr函数的计算结果似乎不同,但为什么呢?这只是其中之一吗 例如:purrr表评价奇异性,r,purrr,R,Purrr,如果列表有或没有标题,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) .fSepal.Length=.l[[c1L,1L]]、萼片.Width=.l[[c2L,1L]
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)
.fSepal.Length=.l[[c1L,1L]]、萼片.Width=.l[[c2L,1L]]、中的错误:未使用的参数萼片.Length=.l[[c1,1]]、萼片.Width=.l[[c2,1]]、花瓣.Length=.l[[c3,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
$Sepal.Length[1]5.1 4.9 4.7 4.6 5.0 5.4
$萼片宽度[1]3.5 3.0 3.2 3.1 3.6 3.9
$Petal.Length[1]1.41.41.31.51.51.41.7
[1] 真的
如果我们只是去掉第二个列表的名称,它就可以正常工作
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
那么我的具体问题是:为什么书名会影响评价方法?有没有什么方法可以在不破坏dplyr管道的情况下进行转换以消除名称?Axeman非常合适,但您可以防御性地动态编程:
library(purrr)
func_b <- function(...) {
args <- list(...)
paste(args[[1]], args[[2]], args[[3]])
}
list(iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length) %>%
pmap(func_b) %>%
head(2)
## [[1]]
## [1] "5.1 3.5 1.4"
##
## [[2]]
## [1] "4.9 3 1.4"
iris[,1:3] %>% pmap(func_b) %>% head(3)
## [[1]]
## [1] "5.1 3.5 1.4"
##
## [[2]]
## [1] "4.9 3 1.4"
##
## [[3]]
## [1] "4.7 3.2 1.3"
func_c <- function(...) {
args <- list(...)
paste0(args, collapse = " ")
}
list(iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length) %>%
pmap(func_c) %>%
head(2)
## [[1]]
## [1] "5.1 3.5 1.4"
##
## [[2]]
## [1] "4.9 3 1.4"
iris[,1:3] %>% pmap(func_c) %>% head(3)
## [[1]]
## [1] "5.1 3.5 1.4"
##
## [[2]]
## [1] "4.9 3 1.4"
##
## [[3]]
## [1] "4.7 3.2 1.3"
iris[,1:2] %>% pmap(func_c) %>% head(3)
## [[1]]
## [1] "5.1 3.5"
##
## [[2]]
## [1] "4.9 3"
##
## [[3]]
## [1] "4.7 3.2"
未命名列表按位置分配给函数参数,按名称分配给命名列表。这意味着purrr正在尝试执行func_bSepal.Length=…..等操作。如果需要,您可以随时使用管道输入unname。很好,unname工作得很好:mapiris[,1:3],list%>%unname%>%pmapfunc_b%>%maphHeadRelevant:因此您基本上是在函数本身中显式地使列表未命名,这可以保护您不受发送的列表类型的影响,这是正确的吗?是的。对的并尝试将其用于任何pmap调用
class(list(iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length)) == class(as.list(iris[,1:3]))
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)
library(purrr)
func_b <- function(...) {
args <- list(...)
paste(args[[1]], args[[2]], args[[3]])
}
list(iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length) %>%
pmap(func_b) %>%
head(2)
## [[1]]
## [1] "5.1 3.5 1.4"
##
## [[2]]
## [1] "4.9 3 1.4"
iris[,1:3] %>% pmap(func_b) %>% head(3)
## [[1]]
## [1] "5.1 3.5 1.4"
##
## [[2]]
## [1] "4.9 3 1.4"
##
## [[3]]
## [1] "4.7 3.2 1.3"
func_c <- function(...) {
args <- list(...)
paste0(args, collapse = " ")
}
list(iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length) %>%
pmap(func_c) %>%
head(2)
## [[1]]
## [1] "5.1 3.5 1.4"
##
## [[2]]
## [1] "4.9 3 1.4"
iris[,1:3] %>% pmap(func_c) %>% head(3)
## [[1]]
## [1] "5.1 3.5 1.4"
##
## [[2]]
## [1] "4.9 3 1.4"
##
## [[3]]
## [1] "4.7 3.2 1.3"
iris[,1:2] %>% pmap(func_c) %>% head(3)
## [[1]]
## [1] "5.1 3.5"
##
## [[2]]
## [1] "4.9 3"
##
## [[3]]
## [1] "4.7 3.2"