简体   繁体   中英

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() .

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?

We can use lengths to calculate the length of nested list

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

dat$nof_values <- lengths(dat$summary)

Side note : length is different from 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)

dat <- iris %>% 
  group_by(Species) %>% 
  summarise(summary = list(fivenum(Petal.Width))) %>% 
  mutate(nof_value = map_int(summary, length))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM