计算数据框中的每一行

时间:2017-03-09 21:45:06

标签: r data.table

给定数据框

function clusterCallBack(cluster)
{
         var contained = primitives[i].containedPushpins;
         for(var j = 0;j < contained.length;j++)
         {
            contained[j].metadata = { cluster: cluster };
         }
}

我想计算下面给出的每一行,并将其总结为模式&#34;斑点&#34;和#34;没有斑点&#34; -

  ID days dose1 dose2 dose3 dose4  pattern
1 TM    2  11.0    45   0.2   0.1    spots
2 ZZ   18   2.0     6   8.0   0.0 no spots
3 YY    5   0.4     8  10.0  20.0 no spots
4 GG    5   0.4     8  10.0  20.0    spots


df <- structure(list(ID = c("TM", "ZZ", "YY", "GG"), days = c(2L, 18L, 
5L, 5L), dose1 = c(11, 2, 0.4, 0.4), dose2 = c(45L, 6L, 8L, 8L
), dose3 = c(0.2, 8, 10, 10), dose4 = c(0.1, 0, 20, 20), pattern = c("spots", 
"no spots", "no spots", "spots")), .Names = c("ID", "days", "dose1", 
"dose2", "dose3", "dose4", "pattern"), row.names = c(NA, -4L), class = "data.frame")

library(data.table)
setDT(df)

无论如何,我可以按照上面的每行计算并将其添加到数据框dfx中吗?

2 个答案:

答案 0 :(得分:2)

无需将计算保存到单独的data.frame

df[, paste0("Cal", 1:4) := .(sum(dose1>0)/days, 
                             sum(dose2>0)/days, 
                             sum(dose3>0)/days, 
                             sum(dose4>0)/days), by = pattern]
df
#   ID days dose1 dose2 dose3 dose4  pattern      Cal1      Cal2      Cal3       Cal4
#1: TM    2  11.0    45   0.2   0.1    spots 1.0000000 1.0000000 1.0000000 1.00000000
#2: ZZ   18   2.0     6   8.0   0.0 no spots 0.1111111 0.1111111 0.1111111 0.05555556
#3: YY    5   0.4     8  10.0  20.0 no spots 0.4000000 0.4000000 0.4000000 0.20000000
#4: GG    5   0.4     8  10.0  20.0    spots 0.4000000 0.4000000 0.4000000 0.40000000

答案 1 :(得分:2)

如果您有许多dose1dose2等列,那么为每个输出列Cal1Cal2等编写单独的表达式会变得很麻烦。相反,data.table语法允许简洁地编写

df[, paste0("Cal", 1:4) := lapply(.SD, function(x) sum(x > 0) / days), 
   by = pattern, .SDcols = paste0("dose", 1:4)]
df
#   ID days dose1 dose2 dose3 dose4  pattern      Cal1      Cal2      Cal3       Cal4
#1: TM    2  11.0    45   0.2   0.1    spots 1.0000000 1.0000000 1.0000000 1.00000000
#2: ZZ   18   2.0     6   8.0   0.0 no spots 0.1111111 0.1111111 0.1111111 0.05555556
#3: YY    5   0.4     8  10.0  20.0 no spots 0.4000000 0.4000000 0.4000000 0.20000000
#4: GG    5   0.4     8  10.0  20.0    spots 0.4000000 0.4000000 0.4000000 0.40000000