【问题标题】:purrr::map not looping through groups when using vegan function (poolacuum)purrr::map 使用纯素函数(poolacuum)时不循环组
【发布时间】:2020-10-22 09:20:58
【问题描述】:

我正在尝试使用purrr::map 迭代地映射数据框架内的不同组(例如夏季与冬季调查),并计算每个组的物种积累指数(按夏季/冬季)。

# 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) 我该如何解决这个问题?

【问题讨论】:

    标签: r dplyr purrr vegan


    【解决方案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   
    

    【讨论】:

      猜你喜欢
      • 2019-12-20
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 2021-04-26
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多