嵌套在R中的sapply [英] nested sapply in R - breakdown

查看:103
本文介绍了嵌套在R中的sapply的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章与我以前的问题有关从嵌套列表中提取数据,此问题已得到解答.答案之一包含sapply函数:

This post is related to my previous question about extracting data from nested lists, which has been answered. One of the answers contains a sapply function:

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

我对data.table非常陌生,并应用了各种功能并努力理解:

I am very new to data.table and apply functions and struggle to understand:

此特定代码行中发生了什么?

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

Why does cooking appear twice in the lists after running usageExists ?

sapply

What is the purpose of the argument f in the function within sapply

数据的结构和结果如下:

The structure and results of the data are provided below:

> 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语句.

Well, this is essentially a nested loop. sapply(x, function(f) ...) simply takes each element in x and passes it as the argument f to the function. That function in your case is just another sapply statement.

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

So, usageExist <- sapply(garden$fruit, function(f){...} simply passes each fruit in garden to the function. In your case, this affects names(productFruit$type[[**f**]][["usage"]]. For instance, for the first one, it passes Fruit 1124 from garden into the second sapply, where productFruit$type[[f]] looks up Fruit 1124 from productFruit, and in particular the usage element of that list.

另一方面,第二个sapply接受garden$usage的每个元素,并将其传递给%in%函数.您获得两次cooking的原因是,正如您在str输出中所看到的那样,该数据在该数据中出现了两次,这很有意义,因为您可以烹饪多种水果和蔬菜,而不仅仅是一种.

The second sapply, on the other hand, takes every element of garden$usage and passes it to the %in% function. You get cooking twice because, as you can see in your str output, it appears twices in that data, which makes sense as you can cook a variety of fruits and vegetables, and not just one.

这篇关于嵌套在R中的sapply的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆