简体   繁体   English

将多个函数应用于R中的数据表的多列

[英]Apply multiple functions to multiple columns of data table in R

I need help to remove a loop in a function using data table.我需要帮助来删除使用数据表的函数中的循环。

The code:编码:

f = function(DT, col_by, col_func, col_new, func){
  temp = DT[, mget(unique(c(col_by,col_func)))]
  
##loop to remove
  for ( i in 1:length(func)){
    temp[ ,eval(col_new[i]) := do.call(func[[i]], lapply(col_func, function(x) get(x))), by = mget(col_by)]
  }
##

  temp = unique(temp[,mget(c(col_new, col_by))])
  return(temp)
}

DT = data.table(iris)

col_by = c("Species")
col_func = c("Petal.Length","Petal.Width")
col_new = c("PL.mean","PL.max")
func = list(function(x,y) return(mean(x[y == max(y)])), function(x,y) return(max(x[y == max(y)])))

f(DT, col_by, col_func,col_new,func)

If you have any idea to used data table tricks to remove this loop, let me know!如果您对使用数据表技巧来消除此循环有任何想法,请告诉我!

Thanks a lot for your help :)非常感谢你的帮助 :)

With another lapply :与另一个lapply

f = function(DT, col_by, col_func, col_new, func){
  temp = DT[, mget(unique(c(col_by,col_func)))]
  
  ##loop to remove
  # 1:length(func)){
  #   temp[ ,eval(col_new[i]) := do.call(func[[i]], lapply(col_func, function(x) get(x))), by = mget(col_by)]
  # }
  ##

  temp[ ,(col_new) := lapply(func, function(y) do.call(y, lapply(col_func, function(x) get(x)))), by = mget(col_by)]
  
  temp = unique(temp[,mget(c(col_new, col_by))])
  return(temp)
}

DT = data.table(iris)

col_by = c("Species")
col_func = c("Petal.Length","Petal.Width")
col_new = c("PL.mean","PL.max")
func = list(function(x,y) return(mean(x[y == max(y)])), function(x,y) return(max(x[y == max(y)])))

f(DT, col_by, col_func,col_new,func)
#>     PL.mean PL.max    Species
#>       <num>  <num>     <fctr>
#> 1: 1.600000    1.6     setosa
#> 2: 4.800000    4.8 versicolor
#> 3: 5.933333    6.1  virginica

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将多个函数应用于 data.table 中的多列 - Apply multiple functions to multiple columns in data.table R-将操作应用于数据表中的多个指定列 - R - Apply operation to multiple specified columns in data table 如何通过 R 中的多个列将 function 应用于 data.table 子集? - How to apply a function to a data.table subset by multiple columns in R? R:如何将用户定义的函数应用于数据表的多列并在滑动窗口中进行评估,即移动平均 - R: How to apply user defined functions to multiple columns of data table and evaluate in a sliding window, i.e. moving average 以编程方式将不同的函数应用于 data.table R 中的不同列 - Apply different functions to different columns programmatically in data.table R R - 应用 diff() function 或等效的自定义 function 在 Z1639B13BE20377B20 中的多个列上 - R - apply diff() function or equivalent self-defined function on multiple columns in a data.table R函数用多个函数汇总多列数据,按列分组 - R function to summarize multiple columns of data with multiple functions, grouped by a column 将 function 应用于 R 中的多个列 - Apply a function to multiple columns in R R将功能应用于多列 - R apply function to multiple columns r-具有多个列的ifelse(应用) - r - ifelse with multiple columns (apply)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM