创建一个函数来处理我的数据框计算

阿米尔

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

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))

另一件事是,先前的代码在所有时间点都假定主题ID = 1。如果WT(权重)更改为120时主题ID = 2。如何重置计算并使数据框中的所有主题ID自动进行计算?在这种情况下,原始数据帧将如下所示:

#code:
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,"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
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)]
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))

先感谢您!

善子

通常,当对不同主题的数据进行计算时,我喜欢按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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

当我创建一个函数来处理xhrhttp请求时,如何解决未捕获的类型错误?

创建一个函数来处理通用类型的数据项并转换为列表

有没有办法创建一个中央程序来处理我计算机的不同 RGB 组件?

创建一个函数来处理Google表格中的RSS提要

如何快速创建一个 Network 类来处理我所有的数据库 Firestore 操作并且可以从任何视图控制器调用?

创建一个函数来执行计算并将答案作为列添加到现有数据框

一个 JS 函数来处理来自 3 个滑块的输出

在Pandas中,如何分配一个函数来处理列上的字符串?

编写一个函数来处理C#中的异常

如何创建一个函数来计算年份?

创建一个函数来接收 R 中的数据框名称和列

创建一个函数来计算一个没有内置方法的数字的对数

你能做一个函数来处理zsh上的权限被拒绝,比如找不到命令吗

如何为数据框创建一个函数来检查大元组是否包含小元组的所有元素?

我可以制作一个函数来使用循环制作这样的数据框吗?(后续问题)

创建一个快速的帮助程序类来处理CoreLocation函数

我是否必须创建一个类来处理应用程序级事件?

尝试创建一个函数来计算超过 6 个月而不是一年

创建一个函数来计算给定向量的偏度

如何创建一个单独的类来处理android常见任务(如创建进度对话框)?

我需要一个函数来显示“未处理”的表达式

创建一个矢量函数来清除休斯顿犯罪数据的地址数据

我正在创建一个函数来搜索文件中的“唯一”单词

在 DAX 中,我可以创建一个函数来计算两个日期/时间字段之间的工作日吗?

需要一个R函数来从数据框中选择特定的命名列

我正在尝试创建一个函数来计算向量中一组内所有对组合的百分比差异

创建一个循环或函数来收集使用r?中另一个数据帧中的一个数据帧中的其他列值计算的多个向量的值。

有没有办法编写一个模板函数来处理智能指针和常规指针?

我该如何创建一个函数来检测可滚动的正文何时溢出?