如何将for循环写入函数。如何按主题ID应用它

时间:2014-09-18 08:48:43

标签: r dataframe

我有一个数据框(下表):我有一个代码来计算A1和A2的值并将它们填入数据框中。这是我为主题计算A1,A2的代码。

#Set parameter values needed for calcuation
k10 <- 0.2
k12 <- 0.1
k21 <- 0.04
k20 <- 0
E1 <- k10+k12
E2 <- k21+k20
lambda1 = 0.3145683
lambda2 = 0.02543168

#Matrix calculations
for(i in 2:nrow(df))
{

 t <- df$TIME[i]-df$TIME[i-1]
 A1last <- df$A1[i-1] 
 A2last <- df$A2[i-1]
 A1term = (((A1last*E2+A2last*k21)-A1last*lambda1)*exp(-t*lambda1)-((A1last*E2+A2last*k21)-A1last*lambda2)*exp(-t*lambda2))/(lambda2-lambda1)
 df$A1[i] = A1term + df$AMT[i]    

 A2term = (((A2last*E1+A1last*k12)-A2last*lambda1)*exp(-t*lambda1)-((A2last*E1+A1last*k12)-A2last*lambda2)*exp(-t*lambda2))/(lambda2-lambda1)
 df$A2[i] = A2term

 }

如果有人帮助我,我将不胜感激: 1)对每个ID应用上面的矩阵计算(即计算A1,A2为ID = 1,然后重置并计算A1,A2为ID = 2并填入数据帧) 2)将矩阵计算放在函数中。

以下是我的数据帧的样子(注意:在R中创建此df的代码也在下面提供)

df <- ID TIME AMT   A1  A2  DV  WT
1   0   100 100 0   NA  70
1   1   0   NA  NA  NA  70
1   2   0   NA  NA  NA  70
1   3   0   NA  NA  NA  70
1   4   0   NA  NA  NA  70
1   5   0   NA  NA  NA  70
1   6   100 NA  NA  NA  70
1   6   0   NA  NA  NA  70
1   7   0   NA  NA  NA  70
1   8   0   NA  NA  NA  70
1   9   0   NA  NA  NA  70
1   10  0   NA  NA  NA  70
1   11  0   NA  NA  NA  70
2   0   50  50  0   NA  120
2   1   0   NA  NA  NA  120
2   2   0   NA  NA  NA  120
2   3   0   NA  NA  NA  120
2   4   0   NA  NA  NA  120
2   5   0   NA  NA  NA  120
2   6   0   NA  NA  NA  120
2   7   50  NA  NA  NA  120
2   8   0   NA  NA  NA  120
2   9   0   NA  NA  NA  120
2   10  0   NA  NA  NA  120
2   11  0   NA  NA  NA  120
2   12  0   NA  NA  NA  120
2   13  0   NA  NA  NA  120
2   14  0   NA  NA  NA  120
2   15  0   NA  NA  NA  120
2   16  0   NA  NA  NA  120
2   17  0   NA  NA  NA  120
2   18  0   NA  NA  NA  120
2   19  0   NA  NA  NA  120
2   20  0   NA  NA  NA  120

rm(list=ls(all=TRUE))
dosetimes <- c(0,6,12,18)
df <-      data.frame("ID"=1,"TIME"=sort(unique(c(seq(0,30,1),dosetimes))),"AMT"=0,"A1"=NA,"A2"=NA,"DV"=NA,"WT"=NA)
doserows <- subset(df, TIME%in%dosetimes)
doserows$AMT[doserows$TIME==dosetimes[1]] <- 100 
doserows$AMT[doserows$TIME==dosetimes[2]] <- 100
doserows$AMT[doserows$TIME==dosetimes[3]] <- 50
doserows$AMT[doserows$TIME==dosetimes[4]] <- 50
df <- rbind(df,doserows)
df <- df[order(df$TIME,-df$AMT),]       
df <- subset(df, (TIME==0 & AMT==0)==F)
df$WT <- 70                    
df$WT[df$TIME >= 12] <- 120 
df$ID[(df$WT>=120)==T] <- 2
df$TIME[df$ID==2] <- c(seq(0,20,1))
df$A1[df$TIME==0] <- df$AMT[(df$TIME ==0)]
df$A2[df$TIME==0] <- 0

提前谢谢!

1 个答案:

答案 0 :(得分:0)

尝试:

fun1 <- function(df){
for(i in 2:nrow(df))
{
 t <- df$TIME[i]-df$TIME[i-1]
 A1last <- df$A1[i-1] 
A2last <- df$A2[i-1]
A1term = (((A1last*E2+A2last*k21)-A1last*lambda1)*exp(-t*lambda1)-((A1last*E2+A2last*k21)-A1last*lambda2)*exp(-t*lambda2))/(lambda2-lambda1)
 df$A1[i] = A1term + df$AMT[i]    
 A2term = (((A2last*E1+A1last*k12)-A2last*lambda1)*exp(-t*lambda1)-((A2last*E1+A1last*k12)-A2last*lambda2)*exp(-t*lambda2))/(lambda2-lambda1)
 df$A2[i] = A2term
 }
df
}

res <- unsplit(lapply(split(df, df$ID), function(x) fun1(x)), df$ID)

 head(res)
 #   ID TIME AMT        A1        A2 DV WT
 #110  1    0 100 100.00000  0.000000 NA 70
 #2    1    1   0  74.24376  8.466067 NA 70
 #3    1    2   0  55.40806 14.434584 NA 70
 #4    1    3   0  41.62585 18.584970 NA 70
 #5    1    4   0  31.53396 21.413134 NA 70
 #6    1    5   0  24.13714 23.281013 NA 70