2017-06-20 9 views
2

リストがタイトルを持っているかどうかにかかわらず、なぜpurrr関数が評価されるようですが、なぜですか?これはちょうど「それらのものの1つ」ですか?purrrリストの評価strangeness

例:

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 .L Petal.Length = .l [[c(3、1)]])

この例では、engl = .l [[c(1,1)]、Sepal.Width = .l [[c(2,1)]]

しかし、唯一の違いは、リストの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] TRUE

私たちは第二のリストの名前を取り除く場合、それはなしで動作
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"

だから私の特定質問:タイトルが評価方法に影響するのはなぜですか?名前を削除するためにdplyrパイプを壊さずに物を変換する方法はありますか?

+2

名前リストが名前でリストという名前の位置によって関数の引数に割り当てられます。つまり、 'purrr'は' func_b(Sepal.Length = .....など) 'をしようとしています。必要に応じて 'unname'にパイプすることができます。 – Axeman

+0

いいえ、unnameはきれいに動作します: 'map(iris [、1:3]、list)%>%unname()%>%pmap(func_b)%>%map(head)' –

答えて

4

Axemanがスポットオンですが、あなたはdefensibly &その周りに動的にプログラムすることができます

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" 
+0

関数自体に名前のないリストが表示されます。これはあなたにnomatterからどのリストタイプが送られてくるのを防ぎますか?これは正しいのですか? –

+1

aye。正しい。どんな 'pmap()'呼び出しでも汎用にしようとしました – hrbrmstr