R使用分组依据和突变创建自定义函数

时间:2018-10-22 21:56:32

标签: r function dplyr

我有数据集并正在执行group_bymutate函数。

但是在使用自定义功能和定义的列(例如Value_1Value_2)时会出错。

请告知我自定义函数中是否可能缺少某些内容

数据集:

library(dplyr)

df <- data.frame(
Date = c("2010-10-06", "2010-10-06", "2010-10-06", "2010-10 
06", "2010-10-06", "2010-10-06", "2010-10-06", "2010-10-06"),
Region = c("Central", "Central", "Central", "Central", "North", "North", 
"North", "North"),
Value_1 = c(10, 2, 4, 12, 4, 4, 2, 15),
Value_2 = c(120, 45, 20, 20, 60, 50, 75, 80),
stringsAsFactors = F)

工作正常:

df %>%
group_by(Date, Region) %>%
mutate(Value_3 = sum(Value_1)) %>%
ungroup()

自定义功能出错:

test_fn <- function(dataset, Col1) {  
dataset <- dataset %>%
group_by(Date, Region) %>%
mutate(Value_3 =  sum(Col1)) %>%
ungroup()  
return(dataset)
}

df_3 <- test_fn(df, "Value_1")

2 个答案:

答案 0 :(得分:1)

test_fn <- function(dataset, Col1) {  
  Col1 = sym(Col1)
  dataset <- dataset %>%
    group_by(Date, Region) %>%
    mutate(Value_3 =  sum(!!Col1)) %>%
    ungroup()  
  return(dataset)
}

如果将sym(Col1)更改为enquo(Col1),则不需要将Col1作为字符串传递,即test_fn(df, Value_1)

答案 1 :(得分:0)

看看您上半部分的内容,我或某人将完成您解决方案的下半部分。您需要了解标准vs non-standard evaluation

tfn <- function(data, col, groups) {
  temp <- data %>%
    ## this gets you to group by the variables 
    ## you need to group by in a standard evaluation way
    group_by_(.dots = groups) %>%
    ## now do a mutate with the dynamic variable name
   ## mutate_(.dots and setName(value, var name)

  temp
}

tfn(df, "Value_1", c("Date", "Region"))