2

我想从R中保存下表:

x <- rnorm(100)
y <- rnorm(100)
z <- rnorm(100)
model <- z ~ x + y
results <- glm(model)
pe <- results$coefficients
vc <- vcov(results)
se <- sqrt(diag(vc))
results.table <- round(cbind(pe, se),3)
rownames(results.table) <- c("Intercept", "X-Estimate", "Y-Estimate")
colnames(results.table) <- c("Parameter", "pe", "Se")
write.table(results.table,file="test.csv",row.names=T,col.names=NA,sep=",")

但是,我无法为行名提供列名:

colnames(results.table) <- c("pe", "Se")     # works
colnames(results.table) <- c("Param", "pe", "Se")    # Doesn't work, incorrect length

更新:答案

根据评论,这是不可能的。如果您有兴趣,这是一种迂回的方式:

rows <- c("Intercept", "X-Estimate", "Y-Estimate")
results.table <- round(cbind(pe, se),3)
results.table <- cbind(rows,results.table)
colnames(results.table) <- c("Parameter", "pe", "Se")
write.table(results.table,file="test.csv",row.names=F,sep=",",quote=F)
4

1 回答 1

3

比您在问题的“更新”中建议的要短一点:

write.csv(cbind(Parameter = rownames(results.table), results.table),
          file = "test.csv", row.names = FALSE)
于 2012-11-18T01:23:02.000 回答