purrr :: map не перебирает группы при использовании веганской функции (poolacuum)

Я пытаюсь использовать purrr::map для итеративного сопоставления различных групп в рамках data.frame (например, летние и зимние исследования) и расчета индексов накопления видов по каждой группе (летом / зимой).

# Make some fake data 
df <- data.frame(season  = c("summer", "summer", "summer",
                             "winter", "winter", "winter"),
                 sp_1    = c(7, 11, 6,
                             0, 0, 0),
                 sp_2    = c(29, 13, 19, 
                             0, 0, 1),
                 sp_3    = c(1, 0, 0, 
                             0, 0, 0)
)
# Attempt to split df by summer/winter (season column) and iteratively calculate species accumulation indices. 
# While it appears the 'group_by' and 'nest' does split the df, 
# applying the 'map' code seems to be working on the entire df (calculates indices NOT by season). 

sac_by_group <- df %>%
  # Which groups do you want different SAC's for? 
  dplyr::group_by(season) %>%
  # Splits the data into the different groups 
  tidyr::nest() %>%
  # Run the species accumulation curves by group
  dplyr::mutate(data = purrr::map(data, 
                                  ~ vegan::poolaccum(df[,2:4]))) %>%
  # Extract the observed species richness estimator (denoted by S)
  dplyr::mutate(data_df = purrr::map(data,
                                     ~ data.frame(summary(.)$S,
                                                  check.names = FALSE))) %>%
  # Drop unnecessary columns
  dplyr::select(-c(data)) %>%
  # Convert the lists back into a data frame
  unnest(cols = c(data_df))
sac_by_group

(1) Как мои кривые накопления рассчитываются для всего набора данных, а не для групп, как предполагалось?

(2) Как мне решить эту проблему?


person Guy Sutton    schedule 22.10.2020    source источник


Ответы (1)


dplyr::mutate(data = purrr::map(data, ~vegan::poolaccum(df[,2:4]))) относится к исходному df, а не к столбцу data.

Должен быть

dplyr::mutate(data = purrr::map(data, vegan::poolaccum))

Мне также пришлось установить minsize = 2, иначе будет ошибка.

df %>%
  # Which groups do you want different SAC's for? 
  dplyr::group_by(season) %>%
  # Splits the data into the different groups 
  tidyr::nest() %>%
  # Run the species accumulation curves by group
  dplyr::mutate(data = purrr::map(data, vegan::poolaccum, minsize = 2)) %>%
  # Extract the observed species richness estimator (denoted by S)
  dplyr::mutate(data_df = purrr::map(data,
                                     ~ data.frame(summary(.)$S,
                                                  check.names = FALSE))) %>%
  # Drop unnecessary columns
  dplyr::select(-c(data)) %>%
  # Convert the lists back into a data frame
  unnest(cols = c(data_df))
 'nperm' >= set of all permutations: complete enumeration.
#> Set of permutations < 'minperm'. Generating entire set.
#> 'nperm' >= set of all permutations: complete enumeration.
#> Set of permutations < 'minperm'. Generating entire set.
#> # A tibble: 4 x 6
#> # Groups:   season [2]
#>   season     N     S `2.5%` `97.5%` Std.Dev
#>   <chr>  <dbl> <dbl>  <dbl>   <dbl>   <dbl>
#> 1 summer     2   2.6    2         3   0.548
#> 2 summer     3   3      3         3   0    
#> 3 winter     2   0.8    0.1       1   0.447
#> 4 winter     3   1      1         1   0   
person Paul    schedule 22.10.2020