R和数据表使用定制函数来创建新输出,而不是ddply

问题描述 投票:0回答:1

我对使用data.table相当陌生,但我需要优化大型仿真的后处理。我本来会使用ddply来根据自定义函数(estimate_AUC)获得具有所需计算参数的新输出,该函数可以容纳不同的列名(例如TIME和Cc)和不同的计算方法(例如last,inf等):

AUC_out <- plyr::ddply(sim, c("ID","Dose"), function(x) {
  out <- data.frame(AUCinf = estimate_AUC(Time = x$TIME,
                                          Conc = x$Cc,
                                          AUCtype = "inf"),
                    AUC48  = estimate_AUC(Time = x$TIME[x$TIME<=48],
                                          Conc = x$Cc[x$TIME<=48],
                                          AUCtype = "last")),
  Cc48  =approx(x$TIME,x$Cc,48)$y,
  stringsAsFactors = FALSE)
})

带有sim:

ID          Cc    TIME Dose
    1:   1 0.000000000    0.00  100
    2:   1 0.462881773    0.25  100
    3:   1 0.625713766    0.50  100
    4:   1 0.729046515    0.75  100
    5:   1 0.825169830    1.00  100
   ---                             

如何使用data.table使用自定义函数,同时能够在函数参数中提供方法,特定列名

dput(head(sim))
structure(list(ID = c(1, 1, 1, 1, 1, 1), DoseID = c(1L, 1L, 1L, 
1L, 1L, 1L), Dose = c(100, 100, 100, 100, 100, 100), nbrDoses = c(1, 
1, 1, 1, 1, 1), ExpID = c(1, 1, 1, 1, 1, 1), TrialID = c(1L, 
1L, 1L, 1L, 1L, 1L), IndivID = c(1L, 1L, 1L, 1L, 1L, 1L), USUBJID = c(11, 
11, 11, 11, 11, 11), TIME = c(0, 0.25, 0.5, 0.75, 1, 1.25), Cc = c(0, 
0.462881773273397, 0.625713765604934, 0.729046515431686, 0.825169830220163, 
0.92030770178198), PL = c(14.8635310605163, 14.8810310604533, 
14.8985310551099, 14.916031006317, 14.9335308009029, 14.9510302005905
), Eff = c(5.19411550856408e-19, 1.18067555547615e-08, 4.21253176904848e-07, 
2.63818207596035e-06, 9.25475212778715e-06, 2.43639651038346e-05
)), class = c("data.table", "data.frame"), row.names = c(NA, 
-6L), .internal.selfref = <pointer: 0x00000000045e1ef0>)
r function data.table plyr
1个回答
0
投票

这里是使用PKNCA包的示例。由于只有一种剂量和一个ID,因此没有太多数据可以计算...

library(data.table)
library(PKNCA)
sim <- structure(list(ID = c(1, 1, 1, 1, 1, 1), 
                      DoseID = c(1L, 1L, 1L, 1L, 1L, 1L), Dose = c(100,100, 100, 100, 100, 100), nbrDoses = c(1, 1, 1, 1, 1, 1), 
                      ExpID = c(1, 1, 1, 1, 1, 1), 
                      TrialID = c(1L, 1L, 1L, 1L, 1L, 1L), 
                      IndivID = c(1L, 1L, 1L, 1L, 1L, 1L), 
                      USUBJID = c(11, 11, 11, 11, 11, 11), TIME = c(0, 0.25, 0.5, 0.75, 1, 1.25), 
                      Cc = c(0, 0.462881773273397, 0.625713765604934, 0.729046515431686, 0.825169830220163, 0.92030770178198), 
                      PL = c(14.8635310605163, 14.8810310604533, 14.8985310551099, 14.916031006317, 14.9335308009029, 14.9510302005905), 
                      Eff = c(5.19411550856408e-19, 1.18067555547615e-08, 4.21253176904848e-07, 2.63818207596035e-06, 9.25475212778715e-06, 2.43639651038346e-05)), class = c("data.table", "data.frame"), row.names = c(NA, -6L))
setDT(sim)
sim[, .(AUC.inf = pk.calc.auc(Cc, TIME, interval=c(0, Inf)),
        AUC.48 = pk.calc.auc(Cc, TIME, interval=c(0, 48)),
        Cc48 = approx(TIME, Cc, 48)$y
        ), by = c("ID", "Dose")]
#>    ID Dose   AUC.inf    AUC.48 Cc48
#> 1:  1  100 0.7757414 0.7757414   NA

reprex package(v0.3.0)在2020-03-22创建

© www.soinside.com 2019 - 2024. All rights reserved.