Search code examples
rdplyrpurrrvegan

purrr::map not looping through groups when using vegan function (poolacuum)


I am trying to use purrr::map to iteratively map over different groups within a data.frame (e.g. summer vs winter surveys), and calculate species accumulation indices by each group (by summer/winter).

# 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) How are my accumulation curves being calculated for the entire dataset, instead of by groups as intended?

(2) How do I solve this issue?


Solution

  • dplyr::mutate(data = purrr::map(data, ~vegan::poolaccum(df[,2:4]))) is referring to the original df and not the data column.

    It should be

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

    I also had to set minsize = 2 otherwise it would error.

    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