purrr::map 使用 vegan 函数 (poolacuum) 时不循环遍历组
purrr::map not looping through groups when using vegan function (poolacuum)
我正在尝试使用 purrr::map
迭代映射 data.frame 中的不同组(例如夏季与冬季调查),并计算每个组的物种积累指数(通过 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) 我的累积曲线是如何针对整个数据集计算的,而不是按预期按组计算的?
(2) 如何解决这个问题?
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
我正在尝试使用 purrr::map
迭代映射 data.frame 中的不同组(例如夏季与冬季调查),并计算每个组的物种积累指数(通过 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) 我的累积曲线是如何针对整个数据集计算的,而不是按预期按组计算的?
(2) 如何解决这个问题?
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