R 创建用于处理我的数据帧计算的函数

R 创建用于处理我的数据帧计算的函数,r,dataframe,analysis,R,Dataframe,Analysis,我正在为我创建的数据帧进行系统计算。我有计算代码,但我想: 1) 将其作为函数,并为我创建的数据帧调用它 2) 重置数据帧中下一个ID的计算 我将感谢你在这方面的帮助和建议 使用以下代码在R中创建数据帧: #Create a dataframe dosetimes <- c(0,6,12,18) df <- data.frame("ID"=1,"TIME"=sort(unique(c(seq(0,30,1),dosetimes))),"AMT"=0,"A1"=NA,"WT"=NA)

我正在为我创建的数据帧进行系统计算。我有计算代码,但我想:

1) 将其作为函数,并为我创建的数据帧调用它

2) 重置数据帧中下一个ID的计算

我将感谢你在这方面的帮助和建议

使用以下代码在R中创建数据帧:

#Create a dataframe
dosetimes <- c(0,6,12,18)

df <- data.frame("ID"=1,"TIME"=sort(unique(c(seq(0,30,1),dosetimes))),"AMT"=0,"A1"=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]] <- 100
doserows$AMT[doserows$TIME==dosetimes[4]] <- 100

#Add back dose information
df <- rbind(df,doserows)
df <- df[order(df$TIME,-df$AMT),]       
df <- subset(df, (TIME==0 & AMT==0)==F)

df$A1[(df$TIME==0)] <- df$AMT[(df$TIME ==0)]


#Time-dependent covariate
df$WT <- 70                    
df$WT[df$TIME >= 12] <- 120  

#The calculations are done in a for-loop. Here is the code for it:
#values needed for the calculation
C <- 2     
V  <- 10    
k <- C/V

#I would like this part to be written as a function

for(i in 2:nrow(df))
{

t <- df$TIME[i]-df$TIME[i-1]
A1last <- df$A1[i-1]

df$A1[i] = df$AMT[i]+ A1last*exp(-t*k)
}

head(df)

plot(A1~TIME, data=df, type="b", col="blue", ylim=c(0,150))
#创建数据帧
剂量时间你想要这个吗:

ddf <- data.frame("ID"=1,"TIME"=sort(unique(c(seq(0,30,1),dosetimes))),"AMT"=0,"A1"=NA,"WT"=NA)

myfn = function(df){

    dosetimes <- c(0,6,12,18)
    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]] <- 100
    doserows$AMT[doserows$TIME==dosetimes[4]] <- 100

    #Add back dose information
    df <- rbind(df,doserows)
    df <- df[order(df$TIME,-df$AMT),]       
    df <- subset(df, (TIME==0 & AMT==0)==F)

    df$A1[(df$TIME==0)] <- df$AMT[(df$TIME ==0)]


    #Time-dependent covariate
    df$WT <- 70                    
    df$WT[df$TIME >= 12] <- 120  

    #The calculations are done in a for-loop. Here is the code for it:
    #values needed for the calculation
    C <- 2     
    V  <- 10    
    k <- C/V

    #I would like this part to be written as a function

    for(i in 2:nrow(df))
    {

    t <- df$TIME[i]-df$TIME[i-1]
    A1last <- df$A1[i-1]

    df$A1[i] = df$AMT[i]+ A1last*exp(-t*k)
    }

    head(df)

    plot(A1~TIME, data=df, type="b", col="blue", ylim=c(0,150))

}

myfn(ddf)

ddf一般来说,在对不同主题的数据进行计算时,我喜欢按ID拆分数据帧,将各个主题数据的向量传递到for循环中,进行所有计算,构建一个包含所有新计算数据的向量,然后折叠结果并返回包含所有所需数字的数据帧。这允许你对每一个主题做很多控制

subjects = split(df, df$ID)
forResults = vector("list", length=length(subjects))

# initialize these constants
C <- 2     
V  <- 10    
k <- C/V

myFunc = function(data, resultsArray){
  for(k in seq_along(subjects)){
    df = subjects[[k]]
    df$A1 = 100 # I assume this should be 100 for t=0 for each subject?

    # you could vectorize this nested for loop..
    for(i in 2:nrow(df)) {

      t <- df$TIME[i]-df$TIME[i-1]
      A1last <- df$A1[i-1]

      df$A1[i] = df$AMT[i]+ A1last*exp(-t*k)
    }

    head(df)

    # you can add all sorts of other calculations you want to do on each subject's data

    # when you're done doing calculations, put the resultant into 
    # the resultsArray and we'll rebuild the dataframe with all the new variables
    resultsArray[[k]] = df

    # if you're not using RStudio, then you want to use dev.new() to instantiate a new plot canvas
    # dev.new()   # dont need this if you're using RStudio (which doesnt allow multiple plots open)
    plot(A1~TIME, data=df, type="b", col="blue", ylim=c(0,150))

  }

  # collapse the results vector into a dataframe
  resultsDF = do.call(rbind, resultsArray)
  return(resultsDF)
}

results = myFunc(subjects, forResults)
subjects=split(df,df$ID)
forResults=向量(“列表”,长度=长度(主题))
#初始化这些常量

那么你不知道该怎么做?实际的编程问题在哪里?现在看来这只是一个“请为我做这件事”的请求。我想把for循环写成一个函数,这样我就可以为数据帧调用它;如df2 2:我不知道如何重置下一个主题ID的计算。(即,从ID2的时间=0开始,对ID=2再次执行相同的计算)@MrFlickThank@skotturi。我测试了它,它实际上做了错误的计算。看来我的问题我的问题不够清楚。我再次发布了一个新问题,它更清楚地说明了我想要实现的目标。如果你能看一下,我将不胜感激。()看起来你收到了一个适合你的答案。我应该指出,这个问题的答案和这个答案做的一样,它按主题分割数据,运行所有您想要的计算(您似乎添加了几个变量),然后将整个数据帧重新组合在一起。有一个奇特的软件包叫做plyr,不过当人们学习R时,我通常建议用手把它全部写出来,因为这样更容易看到正在发生的事情(而不是使用unsplit(lapply(split(data,func)))。它们都能工作,只是取决于什么能帮助你学得最好。感谢你的帮助和指导。你完全正确;手动编写代码并理解它会有很大帮助。老实说,我总是很难理解(应用)系列函数。我每天都在学习更多=)
for(i in 1:N) {
    myfn(ddf[ddf$ID==i,])
    readline(prompt="Press <Enter> to continue...") 
}
subjects = split(df, df$ID)
forResults = vector("list", length=length(subjects))

# initialize these constants
C <- 2     
V  <- 10    
k <- C/V

myFunc = function(data, resultsArray){
  for(k in seq_along(subjects)){
    df = subjects[[k]]
    df$A1 = 100 # I assume this should be 100 for t=0 for each subject?

    # you could vectorize this nested for loop..
    for(i in 2:nrow(df)) {

      t <- df$TIME[i]-df$TIME[i-1]
      A1last <- df$A1[i-1]

      df$A1[i] = df$AMT[i]+ A1last*exp(-t*k)
    }

    head(df)

    # you can add all sorts of other calculations you want to do on each subject's data

    # when you're done doing calculations, put the resultant into 
    # the resultsArray and we'll rebuild the dataframe with all the new variables
    resultsArray[[k]] = df

    # if you're not using RStudio, then you want to use dev.new() to instantiate a new plot canvas
    # dev.new()   # dont need this if you're using RStudio (which doesnt allow multiple plots open)
    plot(A1~TIME, data=df, type="b", col="blue", ylim=c(0,150))

  }

  # collapse the results vector into a dataframe
  resultsDF = do.call(rbind, resultsArray)
  return(resultsDF)
}

results = myFunc(subjects, forResults)