[英]nested sapply in R - breakdown
這篇文章與我之前關於從嵌套列表中提取數據的問題有關,該問題已得到解答。 其中一個答案包含一個sapply
函數:
usageExist <- sapply(garden$fruit, function(f){
sapply(garden$usage, '%in%', x = names(productFruit$type[[f]][["usage"]]))})
我對 data.table 和應用函數很陌生並且很難理解:
在這行特定的代碼中發生了什么?
為什么在運行usageExists
后cooking
會在列表中出現兩次?
sapply
函數中參數f
的用途是什么
數據的結構和結果如下:
> str(productFruit)
List of 2
$ Basket: chr "DUH"
$ type :List of 3
..$ Fruit 1124:List of 3
.. ..$ ID : num 1
.. ..$ color: chr "poor"
.. ..$ usage:List of 2
.. .. ..$ eating :List of 3
.. .. .. ..$ ID : num 1
.. .. .. ..$ quality : chr "good"
.. .. .. ..$ calories: num 500
.. .. ..$ medicine:List of 3
.. .. .. ..$ ID : num 2
.. .. .. ..$ quality : chr "poor"
.. .. .. ..$ calories: num 300
..$ Fruit 1068:List of 3
.. ..$ ID : num [1:3] 1 2 3
.. ..$ color: num [1:3] 3 4 5
.. ..$ usage:List of 4
.. .. ..$ eating :List of 3
.. .. .. ..$ ID : num 1
.. .. .. ..$ quality : chr "poor"
.. .. .. ..$ calories: num 420
.. .. ..$ cooking :List of 3
.. .. .. ..$ ID : num 2
.. .. .. ..$ quality : chr "questionable"
.. .. .. ..$ calories: num 600
.. .. ..$ drinking:List of 3
.. .. .. ..$ ID : num 3
.. .. .. ..$ quality : chr "good"
.. .. .. ..$ calories: num 800
.. .. ..$ medicine:List of 3
.. .. .. ..$ ID : num 4
.. .. .. ..$ quality : chr "good"
.. .. .. ..$ calories: num 0
..$ Fruit 1051:List of 3
.. ..$ ID : num [1:3] 1 2 3
.. ..$ color: num [1:3] 3 4 5
.. ..$ usage:List of 3
.. .. ..$ cooking :List of 3
.. .. .. ..$ ID : num 1
.. .. .. ..$ quality : chr "good"
.. .. .. ..$ calories: num 49
.. .. ..$ drinking:List of 3
.. .. .. ..$ ID : num 2
.. .. .. ..$ quality : chr "questionable"
.. .. .. ..$ calories: num 11
.. .. ..$ medicine:List of 3
.. .. .. ..$ ID : num 3
.. .. .. ..$ quality : chr "poor"
.. .. .. ..$ calories: num 55
> str(garden)
Classes ‘data.table’ and 'data.frame': 5 obs. of 3 variables:
$ fruit : chr "Fruit 1124" "Fruit 100" "Fruit 1051" "Fruit 1068" ...
$ usage : chr "cooking" "cooking" "NA" "drinking" ...
$ reported: chr "200" "500" "77" "520" ...
- attr(*, ".internal.selfref")=<externalptr>
> fruitExist <- fruit %in% names(productFruit$type)
> fruitExist
[1] TRUE FALSE TRUE TRUE FALSE
> usageExist <- sapply(garden$fruit, function(f){
+ sapply(garden$usage, '%in%', x = names(productFruit$type[[f]][["usage"]]))}) # return a list of 5
> usageExist
$`Fruit 1124`
cooking cooking NA drinking medicine
[1,] FALSE FALSE FALSE FALSE FALSE
[2,] FALSE FALSE FALSE FALSE TRUE
$`Fruit 100`
$`Fruit 100`$cooking
logical(0)
$`Fruit 100`$cooking
logical(0)
$`Fruit 100`$`NA`
logical(0)
$`Fruit 100`$drinking
logical(0)
$`Fruit 100`$medicine
logical(0)
$`Fruit 1051`
cooking cooking NA drinking medicine
[1,] TRUE TRUE FALSE FALSE FALSE
[2,] FALSE FALSE FALSE TRUE FALSE
[3,] FALSE FALSE FALSE FALSE TRUE
$`Fruit 1068`
cooking cooking NA drinking medicine
[1,] FALSE FALSE FALSE FALSE FALSE
[2,] TRUE TRUE FALSE FALSE FALSE
[3,] FALSE FALSE FALSE TRUE FALSE
[4,] FALSE FALSE FALSE FALSE TRUE
$`Fruit 1`
$`Fruit 1`$cooking
logical(0)
$`Fruit 1`$cooking
logical(0)
$`Fruit 1`$`NA`
logical(0)
$`Fruit 1`$drinking
logical(0)
$`Fruit 1`$medicine
logical(0)
嗯,這本質上是一個嵌套循環。 sapply(x, function(f) ...)
簡單地獲取x
每個元素並將其作為參數f
傳遞給函數。 在您的情況下,該功能只是另一個sapply
語句。
因此, usageExist <- sapply(garden$fruit, function(f){...}
只是將garden
中的每個fruit
傳遞給函數。在您的情況下,這會影響names(productFruit$type[[**f**]][["usage"]]
例如,對於第一個,它通過Fruit 1124
從garden
到第二sapply,其中productFruit$type[[f]]
查找Fruit 1124
從productFruit
,並且特別是usage
的元件那個清單。
另一方面,第二個sapply
獲取garden$usage
每個元素並將其傳遞給%in%
函數。 您可以cooking
兩次,因為正如您在str
輸出中看到的那樣,它在該數據中出現了兩次,這是有道理的,因為您可以烹飪各種水果和蔬菜,而不僅僅是一種。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.