在一个循环中,我很难计算出多个类似的任务。我试图用简单的for循环来操作参数,但每次都失败了。
我在这里分享的例子来自生存分析,但我为所有类似的任务寻求一个解决方案。
我以前用surv_cutpoint()进行了计算,对于用survfit()计算拟合值的步骤,我编写了一个for循环,因为我想分别计算每两个蛋白质水平的总体存活率(OS)和无病生存率(DFS)。
如果我的问题过于基本,我很抱歉,但我找不到答案。
循环是这样
for (i in c("protein1", "protein2")){
for(f in c("OS", "DFS")){
column_name<- paste0(i,"_IHC_Score")
fit_name<-paste0(f,"_fit_",i)
time <- paste0(f,"_month")
status <- paste0(f,"_status")
data_name <- paste0("dummy_", f)
paste(fit_name)<- survfit(
formula = Surv(time, status) ~ column_name,
data = data_name)
}
}我收到错误的Time variable is not numeric,我认为问题是引号,但noquote()没有帮助。
作为一个例子,公式假设这样的工作“蛋白质1”和“整体生存”:
Call: survfit(formula = Surv(OS_month, OS_status) ~ protein1_IHC_Score,
data = dummy_OS)
27 observations deleted due to missingness
n events median 0.95LCL 0.95UCL
protein1_IHC_Score=high 41 7 NA 32 NA
protein1_IHC_Score=low 56 5 NA NA NA其结果是:
数据集是这样的
> head(dummy_OS)
OS_month OS_status protein1_IHC_Score protein2_IHC_Score
1 104 0 low high
2 91 0 high high
3 49 0 low high
4 111 0 low <NA>
5 105 0 low high
6 52 0 high high
> head(dummy_DFS)
DFS_month DFS_status protein1_IHC_Score protein2_IHC_Score
1 104 0 low high
2 91 0 high high
3 49 0 low high
4 111 0 low <NA>
5 105 0 low high
6 52 0 high high发布于 2021-01-05 11:14:00
您可以更改最后一个代码。
paste(fit_name) <- survfit(
formula = Surv(time, status) ~ column_name,
data = data_name
)至
assign(fit_name, survfit(
formula = as.formula(paste0("Surv(", time, ", ", status, ") ~ ", column_name)),
data = eval(parse(text=data_name))
))为了进一步改进,我建议不要将每个结果保存到不同的变量,而是将它们保存到一个单独的列表中。
例如,
map(c(protein1="protein1", protein2="protein2"), function(i){
imap(c(OS=dummy_OS, DFS=dummy_DFS), function(data_name, f){
survfit(
formula = as.formula(paste0(
"Surv(", f, "_month", ", ", f, "_status", ") ~ ", f, "_status"
)),
data = data_name
)
})
})生成嵌套列表,如
$protein1
$protein1$OS
survfit object 1
$protein1$DFS
survfit object 2
$protein2
$protein2$OS
survfit object 3
$protein2$DFS
survfit object 4我希望这能帮上忙!
https://stackoverflow.com/questions/65577098
复制相似问题