【发布时间】:2017-03-07 12:21:45
【问题描述】:
我有一个包含 26 个 .csv 文件的文件夹。每个文件都有两列,标题为 DO2 和 Time_min,并且都至少有 300 多行。
我想用x=Time_min 和y=DO2 制作散点图,分别制作线性模型,为26 个模型中的每一个取coefficient 和R^2 并将其放在一个表格中。
就代码而言,这就是我所写的。我知道我可以复制和粘贴它,但我也知道必须有更聪明的方法。
setwd("~/Documents/Masters/Data/R/35789/35789_Ucrit")
#The file where I want all the coefficients and R^2 to go
UE_Slope <- read.csv("~/Documents/Masters/Data/R/35789/35789_UE_Slope.csv")
temp = list.files(pattern="*.csv")
for (i in 1:length(temp))(assign(temp[i], read.csv(temp[i])))
#Seal# are the names for the files directory, 1-26
plot(DO2 ~ Time_min, data = Seal1)
model1 <- lm(DO2 ~ Time_min, data = Seal1.csv)
UE_Slope <- rbind(UE_Slope, data.frame("Slope"=coef(model1)[[2]], "R.2"=summary(model1)$r.squared))
【问题讨论】:
-
不要使用assign,而是尝试添加到列表中的对象:
lst <- list();lst[[temp[i]]] <- read.csv(temp[i])
标签: r loops regression linear-regression lm