2016-12-06 41 views
0

我正在寻找应用用户根据给定的列名定义功能应用基于列名的函数data.tables [R

dt <- data.table(gr_id = 1, id = seq(1,10),min_c = runif(10,10,30), 
       ml_c = runif(10,30,50),mx_c = runif(10,50,100), 
       min_t = runif(10,10,20),ml_t = runif(10,20,25), 
       mx_t = runif(10,25,30)) 

我想适用的计算(min(min)+min(ml))/mx功能对于“c”列和“t”列。目前,我做了如下。然而,当我要添加更多的列变硬(可以说,“一”)

dt[,{ 
    temp1 = min(min_c) 
    temp2 = min(ml_c) 
    temp3 = min(mx_c) 
    score_c = (temp1+temp2)/temp3 
    temp4 = min(min_t) 
    temp5 = min(ml_t) 
    temp6 = min(mx_t) 
    score_t = (temp4+temp5)/temp6 
    list(score_c = score_c, 
     score_t = score_t) 
},by = gr_id 
    ] 

回答

0

我认为这会工作。基本思想是使用get

# the original code could be simplified to: 
dt[, .(
    score_c = (min(min_c) + min(ml_c))/min(mx_c), 
    score_t = (min(min_t) + min(ml_t))/min(mx_t) 
    ), by = gr_id] 
# 
# gr_id score_c score_t 
# 1:  1 0.9051556 1.28054 

# using `get` 
cols <- c('c', 't') 
dt[, { 
    res <- lapply(cols, function(i){ 
     vars <- paste(c('min', 'ml', 'mx'), i, sep = '_') 
     (min(get(vars[1])) + min(get(vars[2])))/min(get(vars[3])) 
    }) 
    names(res) <- paste('score', cols, sep = '_') 
    res 
}, by = gr_id] 

# gr_id score_c score_t 
# 1:  1 0.9051556 1.28054