-1
我有以下用R编写的代码片段,我应该放在for循环中。我是R新手,所以我需要你的帮助。更改变量并将输出保存在for循环中R
我分解的代码:
进口lmerTest包
library(lmerTest)
负载的表(这应该是外面的for循环
table = read.csv("path/namefile.csv")
table_data = table
table_data[table_data == "<undefined>"] <- NA
na_rows <- is.na(table_data[,4])
table_data_sub <- table_data[!na_rows,]
我计算模型,这应该是for循环和STAGE1每次都会改变,特别是它应该是STAGE1,STAGE2,...,直到STAGE13 所有这些变量都是t ALBE table_data_sub的for循环
TNST.model <- lmer(STAGE1 ~ Patient_type+Gender+Age+(1|Subject),data=table_data,REML=FALSE)
我计算最小二乘装置
TNST.model_ls <-lsmeans(TNST.model)
difflsmeans_TNST<-difflsmeans(TNST.model, test.effs=NULL)
我保存的输出在2个文件应该相应地改变名称到STAGE
开始
out_file <- file("/path/STAGE1_diffmean.csv", open="a") #creates a file in append mode
for (i in seq_along(difflsmeans_TNST)){
write.table(names(difflsmeans_TNST)[i], file=out_file, sep=",", dec=".",
quote=FALSE, col.names=FALSE, row.names=FALSE) #writes the name of the list elements ("A", "B", etc)
write.table(difflsmeans_TNST[[i]], file=out_file, sep=",", dec=".", quote=FALSE,
col.names=NA, row.names=TRUE) #writes the data.frames
}
close(out_file) #close connection to file.csv
out_file <- file("/path/STAGE1_leastmean.csv", open="a") #creates a file in append mode
for (i in seq_along(TNST.model_ls)){
write.table(names(TNST.model_ls)[i], file=out_file, sep=",", dec=".",
quote=FALSE, col.names=FALSE, row.names=FALSE) #writes the name of the list elements ("A", "B", etc)
write.table(TNST.model_ls[[i]], file=out_file, sep=",", dec=".", quote=FALSE,
col.names=NA, row.names=TRUE) #writes the data.frames
}
close(out_file) #close connection to file.csv
for循环结束