如何使此功能动态化?函数

How to make this function dynamic? R function

我在 R 中有一个函数,它生成一个 table 从数据帧中选取数据的图形,每次我想传递一个不同的变量(数据帧中的列名)时,我都必须重复代码。所以有时它可以是变量,有时是变量b,有时是变量c...等等

generates_table_variablea <- function(data) { ## how to pass the column = variablea here like this 
                                              ####### function(data, column = variablea) .. ???
  
  big_data <- data %>%
    group_by(a, b, c, d) %>% 
    mutate(total_categoria_abs = sum(abs(f))) %>%
    mutate(volume_negativo = if_else(variablea < 0, f, 0)) %>% 
    mutate(volume_positivo = if_else(variablea > 0, f, 0)) %>%
    mutate(total = sum(volume_positivo) - sum(volume_negativo)) %>% 
    mutate(e = if_else(variablea < 0, sum(variablea), 0)) %>% 
    ungroup() %>% 
    filter (variablea < 0) %>% 
    group_by(a, b, c, d) %>% 
    summarise(e = mean(e), vendas = sum(f*-1), frac_vendas = vendas*-1/mean(total_categoria_abs)) %>%
    arrange(e) %>% 
    ungroup() 
  
  
  big_data$frac_vendas <- round(big_data$frac_vendas, digits = 2)
  
  big_data$e <- round(big_data$e, digits = 0)
  
}

如果我想更改这个变量,我必须执行以下操作:

generates_table_variableb <- function(data) { ## HERE IT WILL BE function(data, column = variableb)...
  
  big_data <- data %>%
    group_by(a, b, c, d) %>% 
    mutate(total_categoria_abs = sum(abs(f))) %>%
    mutate(volume_negativo = if_else(variableb < 0, f, 0)) %>% #### HERE I NEED TO CHANGE ALWAYS TO VARIABLEA, VARIABLEB, VARIABLEC...
    mutate(volume_positivo = if_else(variableb > 0, f, 0)) %>%
    mutate(total = sum(volume_positivo) - sum(volume_negativo)) %>% 
    mutate(e = if_else(variablea < 0, sum(variableb), 0)) %>% 
    ungroup() %>% 
    filter (variableb < 0) %>% 
    group_by(a, b, c, d) %>% 
    summarise(e = mean(e), vendas = sum(f*-1), frac_vendas = vendas*-1/mean(total_categoria_abs)) %>%
    arrange(e) %>% 
    ungroup() 
  
  
  big_data$frac_vendas <- round(big_data$frac_vendas, digits = 2)
  
  big_data$e <- round(big_data$e, digits = 0)
  
} 

让多个函数做同样的事情会减慢我的代码...

这怎么可能更好?我想要的只是动态传递此列。

这是其中一种方式

library(dplyr)
x <- data.frame(v1=1:3, v2=4:6)
f <- function(data, var1){
  x %>% select(!!var1)
}
f(x, quo(v1))

您可以在https://adv-r.hadley.nz/quasiquotation.html

中查看更多解释

我找到了另一个同样有效的方法:

generates_table_variablea <- function(dataframe, variable) { ## Here pass variable
  
  big_data <- dataframe %>%
    group_by(a, b, c, d) %>% 
    mutate(total_categoria_abs = sum(abs(f))) %>%
    mutate(volume_negativo = if_else(.data[[variable]] < 0, f, 0)) %>% 
    mutate(volume_positivo = if_else(.data[[variable]] > 0, f, 0)) %>%
    mutate(total = sum(volume_positivo) - sum(volume_negativo)) %>% 
    mutate(e = if_else(.data[[variable]] < 0, sum(variablea), 0)) %>% 
    ungroup() %>% 
    filter (.data[[variable]] < 0) %>% 
    group_by(a, b, c, d) %>% 
    summarise(e = mean(e), vendas = sum(f*-1), frac_vendas = vendas*-1/mean(total_categoria_abs)) %>%
    arrange(e) %>% 
    ungroup() 
  
  
  big_data$frac_vendas <- round(big_data$frac_vendas, digits = 2)
  
  big_data$e <- round(big_data$e, digits = 0)
  
}

只需将变量替换为.data[[variable]]即可在函数内部传递任何列。