【问题标题】:apply a function which contains group_by within dplyr funciton to datalist in R将包含 dplyr 函数中的 group_by 的函数应用于 R 中的数据列表
【发布时间】:2019-04-23 16:16:17
【问题描述】:

我有一个这样的 data.list:

list(structure(list(group = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L
), species = structure(c(3L, 3L, 1L, 3L, 3L, 2L, 3L, 1L, 3L, 
1L, 3L, 1L, 3L, 1L, 2L, 4L, 1L, 4L, 2L, 3L, 3L, 3L, 2L, 2L), .Label = 
c("Apiaceae", 
"Ceyperaceae", "Magnoliaceae", "Vitaceae"), class = "factor"), 
N = c(2L, 2L, 3L, 2L, 2L, 1L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 
3L, 1L, 4L, 3L, 4L, 1L, 2L, 2L, 2L, 1L, 1L)), class = "data.frame", 
row.names = c(NA, 
-24L)), structure(list(group = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L), species = structure(c(3L, 3L, 1L, 3L, 3L, 2L, 3L, 1L, 3L, 
1L, 3L, 1L, 3L, 1L, 2L, 4L, 1L, 4L, 2L, 3L, 3L, 3L, 2L, 2L), .Label = 
c("Apiaceae", 
"Ceyperaceae", "Magnoliaceae", "Vitaceae"), class = "factor"), 
N = c(2L, 2L, 3L, 2L, 2L, 1L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 
3L, 1L, 4L, 3L, 4L, 1L, 2L, 2L, 2L, 1L, 1L)), class = "data.frame", 
row.names = c(NA, 
-24L)))

我想将写在 dplyr 包中的 my.fun 应用到这个数据列表中。首先,我按“组”对数据进行分组,并获得 R 中已有的函数的输出,然后将此函数应用于数据列表。但是输出为0。没有任何输出。你能帮我找出错误吗?

 my.fun <- function(x, y){
    group_by(x, !!as.name(group)) %>%
    mutate(out = diversity(N, "shannon")) 
 }

check <- lapply(colnames(list), function(x) {
  my.fun(x$group, x$N)
}) 

非常感谢!

【问题讨论】:

  • 您的my.fun 中的y 参数在哪里。另外,如果没有group参数,那么可以直接使用group_by(x, group),因为group是列名

标签: r function dplyr lapply datalist


【解决方案1】:

假设我们正在传递组列和将diversity 应用为字符串的列,

library(tidyverse)
library(vegan)
my.fun <- function(data, grpCol, divCol) {
       data %>% 
           group_by_at(grpCol) %>%
           mutate(out = diversity(!! rlang::sym(divCol), "shannon"))
           #or use mutate_at
           # mutate_at(vars(divCol), list(out = ~ diversity(., "shannon")))
    }

map(lst1, my.fun, grpCol = "group", divCol = "N")
#[[1]]
# A tibble: 24 x 4
# Groups:   group [3]
#   group species          N   out
#   <int> <fct>        <int> <dbl>
# 1     1 Magnoliaceae     2  1.75
# 2     1 Magnoliaceae     2  1.75
# 3     1 Apiaceae         3  1.75
# 4     1 Magnoliaceae     2  1.75
# 5     1 Magnoliaceae     2  1.75
# 6     1 Ceyperaceae      1  1.75
# 7     2 Magnoliaceae     2  2.06
# 8     2 Apiaceae         3  2.06
# 9     2 Magnoliaceae     2  2.06
#10     2 Apiaceae         3  2.06
# … with 14 more rows

#[[2]]
# A tibble: 24 x 4
# Groups:   group [3]
#   group species          N   out
#   <int> <fct>        <int> <dbl>
# 1     1 Magnoliaceae     2  1.75
# 2     1 Magnoliaceae     2  1.75
# 3     1 Apiaceae         3  1.75
# 4     1 Magnoliaceae     2  1.75
# 5     1 Magnoliaceae     2  1.75
# 6     1 Ceyperaceae      1  1.75
# 7     2 Magnoliaceae     2  2.06
# 8     2 Apiaceae         3  2.06
# 9     2 Magnoliaceae     2  2.06
#10     2 Apiaceae         3  2.06
# … with 14 more rows

注意

identical(lst1[[1]], lst1[[2]])
#[1] TRUE

【讨论】:

  • 非常感谢阿克伦!我在定义组变量时遇到了一点问题。我们可以在 my.function 中的 data %>% 之后集成另一个函数 (group = my.fun0(x)) 吗?
  • @SunRise 你可以这样做,唯一的事情是my.fun0(x) 应该为分组列返回一个字符串名称
  • @SunRise 另外,不确定my.fun0(x) 假设“x”是数据如何自动返回分组列的名称。它还应该有一些其他参数。另外,不清楚为什么要进行多个函数调用
  • 因为应该为 datalist 定义组变量(对于此列表中的每个数据集)。 x 是每个数据集中的一列,其名称为 B_value。
  • @SunRise 你可以使用bind_rows(yourlst)
猜你喜欢
  • 1970-01-01
  • 2015-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多