在函数中保存lmer摘要

时间:2016-06-27 10:42:20

标签: r function save summary lme4

我写了以下函数:

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倍。我的代码出了什么问题?谢谢!

1 个答案:

答案 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)
}