[英]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.