我写了以下函数:
filename <- list.files(path = "path", pattern = ".csv", full.names = TRUE)
analyze <- function(filename) {
dat <- read.csv(file = filename, header = TRUE)
twolevel <- lmer(DV ~ 1 + (1 | item), data = dat, REML = FALSE)
output <- summary(twolevel)
sink("output.txt")
for (i in 1:73) print(output)
sink()
}
for (f in filename) {
analyze(f)
}
Filename包含73个数据帧的列表。不幸的是,我没有得到73个分析中每个分析的摘要,但结果却是73倍。我的代码出了什么问题?谢谢!
答案 0 :(得分:1)
正如@Laterow和@nya所讨论的那样,你所做的改变 -
analyze <- function(filename) {
dat <- read.csv(file = filename, header = TRUE)
twolevel <- lmer(DV ~ 1 + (1 | item), data = dat, REML = FALSE)
sink("output.txt",append = TRUE)
return(summary(twolevel))
}
然后以
运行命令for (f in filename) {
analyze(f)
}