我有一家公司销售各种产品的收入数据(csv文件),其中之一看起来如下所示:
> abc
Order.Week..BV. Product.Number Quantity Net.ASP Net.Price
1 2013-W44 ABCDEF 92 823.66 749
2 2013-W44 ABCDEF 24 898.89 749
3 2013-W44 ABCDEF 243 892.00 749
4 2013-W45 ABCDEF 88 796.84 699
5 2013-W45 ABCDEF 18 744.80 699
现在,我正在拟合一个多元回归模型,其中Net.Price作为Y,Quantity、Net.ASP作为x1和x2。有100多个这样的文件,我正试图使用以下代码完成它:
fileNames <- Sys.glob("*.csv")
for (fileName in fileNames) {
abc <- read.csv(fileName, header = TRUE, sep = ",")
fit <- lm(Net.Price ~ Quantity + Net.ASP, data = abc)
x <- data.frame (abc, summary(fit))
write.csv (x, file = fileName)
}
现在,我理解这一行代码 x <- data.frame (abc, summary(fit))
是错误的,因为它显示了以下错误信息:Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class ""summary.lm"" to a data.frame
,但是我想要将每个csv文件的回归模型摘要写入文件本身。请帮忙。
summary
返回了很多无法转换为向量的“垃圾”,除非你将其转换为list
。 - David Arenburgx <- cbind(abc, t(as.numeric(coefficients(fit)))); names(x)[(length(x) - 2):length(x)] <- names(coefficients(fit))
- David ArenburgError in file(file, ifelse(append, "a", "w")) : cannot open the connection In addition: Warning message: In file(file, ifelse(append, "a", "w")) : cannot open file 'A3G47AV.csv': Permission denied
- souravsarkar59