【问题标题】:nested sapply in R - breakdown嵌套在 R 中的 sapply - 击穿
【发布时间】:2020-07-09 18:34:12
【问题描述】:

这篇文章与我之前的question有关从嵌套列表中提取数据有关,该帖子已得到解答。其中一个答案包含sapply 函数:

usageExist <- sapply(garden$fruit, function(f){
  sapply(garden$usage, '%in%', x = names(productFruit$type[[f]][["usage"]]))}) 

我对 data.table 和应用函数非常陌生,但很难理解:

这行代码发生了什么

为什么cooking在运行usageExists后会在列表中出现两次

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)

【问题讨论】:

    标签: r list function apply lapply


    【解决方案1】:

    嗯,这本质上是一个嵌套循环。 sapply(x, function(f) ...) 简单地获取x 中的每个元素并将其作为参数f 传递给函数。在您的情况下,该功能只是另一个 sapply 语句。

    所以,usageExist &lt;- sapply(garden$fruit, function(f){...} 只是将garden 中的每个fruit 传递给函数。在您的情况下,这会影响names(productFruit$type[[**f**]][["usage"]]。例如,对于第一个,它将Fruit 1124garden 传递到第二个sapply,其中productFruit$type[[f]]productFruit 查找Fruit 1124,特别是该列表的usage 元素。

    另一方面,第二个sapply 获取garden$usage 的每个元素并将其传递给%in% 函数。你得到cooking 两次,因为正如你在str 输出中看到的那样,它在该数据中出现了两次,这是有道理的,因为你可以烹饪各种水果和蔬菜,而不仅仅是一种。

    【讨论】:

    • 函数的参数f呢?
    • 正如我上面写的,它在productFruit$type[[**f**]] 中用于子集productFruit$type
    猜你喜欢
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 2013-10-10
    相关资源
    最近更新 更多