简体   繁体   English

R:XLConnect不传递变量名

[英]R: XLConnect does not pass variable names

I would like to pass the results of a linear model calculation from R to a worksheet in Excel. 我想将线性模型计算的结果从R传递到Excel中的工作表。 For doing this I am using XLConnect with the following code: 为此,我使用带有以下代码的XLConnect:

x <- 1000:2000
y <- 3*x+rnorm(length(x))
fit <- lm(y~x-1)
result <- summary(fit)$coeff

print(result)

require(XLConnect)
wb <- loadWorkbook("/Users/andreas/test1.xls", create = TRUE)
createSheet(wb, name = "test")
writeWorksheet(wb, result, sheet = "test", startRow = 1, startCol = 1)
saveWorkbook(wb)

However, the problem is that XLConnect does not pass the name of the coeffiecient(s) to Excel (here: x) although they are printed out correctly. 但是,问题是,尽管XLConnect正确打印了,但它们没有将系数的名称传递给Excel(此处为x)。

Does anybody has an idea about that issue? 有人对这个问题有想法吗?

I would greatly appreciate any help. 我将不胜感激任何帮助。

Andy 安迪

The variable name in this case ends up as a rowname once result is converted to a data frame. 一旦result转换为数据帧,这种情况下的变量名就以行名结尾。 There is an argument called rownames in writeWorksheet : 有一个叫参数rownameswriteWorksheet

wb <- loadWorkbook("~/Desktop/test1.xls", create = TRUE)
createSheet(wb, name = "test")
writeWorksheet(wb, result, sheet = "test", startRow = 1, startCol = 1,rownames = "var")
saveWorkbook(wb)

You could pass the names of the coefficient table separately to the values: 您可以将系数表的名称分别传递给值:

writeWorksheet(wb, colnames(result), sheet="test", startRow=1, startCol=2)
writeWorksheet(wb, rownames(result), sheet="test", startRow=2, startCol=1)
writeWorksheet(wb, result, sheet="test", startRow=2, startCol=2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM