[英]How to calculate length of vector within a list column (nested)
I have the following code 我有以下代码
library(tidyverse)
dat <- iris %>%
group_by(Species) %>%
summarise(summary = list(fivenum(Petal.Width)))
dat
#> # A tibble: 3 x 2
#> Species summary
#> <fct> <list>
#> 1 setosa <dbl [5]>
#> 2 versicolor <dbl [5]>
#> 3 virginica <dbl [5]>
Basically I used the Iris data, grouped it by Species and then calculated fivenum()
. 基本上我使用了Iris数据,按Species分组然后计算了
fivenum()
。
What I want to do is to simply calculate the length of the summary values: this is what I have tried but it doesn't produce what I expect: 我想要做的只是简单地计算汇总值的长度:这是我尝试过的但它没有产生我期望的东西:
dat %>%
mutate(nof_value = length(summary))
# A tibble: 3 x 3
# Species summary nof_values
# <fct> <list> <int>
#1 setosa <dbl [5]> 3
#2 versicolor <dbl [5]> 3
#3 virginica <dbl [5]> 3
The nof_values
should all be equal to 5. What's the right way to do it? nof_values
应该都等于5.什么是正确的方法呢?
We can use lengths
to calculate the length of nested list 我们可以使用
lengths
来计算嵌套列表的长度
library(tidyverse)
dat %>%
mutate(nof_values = lengths(summary))
# Species summary nof_values
# <fct> <list> <int>
#1 setosa <dbl [5]> 5
#2 versicolor <dbl [5]> 5
#3 virginica <dbl [5]> 5
whose equivalent in base R is 其基数为R的等价物
dat$nof_values <- lengths(dat$summary)
Side note : length
is different from lengths
旁注:
length
与lengths
不同
length(dat$summary)
#[1] 3
lengths(dat$summary)
#[1] 5 5 5
You can use the map_int command from the purrr package (which is part of the tidyverse) 您可以使用purrr包中的map_int命令(这是tidyverse的一部分)
dat <- iris %>%
group_by(Species) %>%
summarise(summary = list(fivenum(Petal.Width))) %>%
mutate(nof_value = map_int(summary, length))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.