将列名分配给第一列R表

时间:2012-11-18 00:29:51

标签: r export-to-csv

我想将下表保存在 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=",")

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

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)

1 个答案:

答案 0 :(得分:3)

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

write.csv(cbind(Parameter = rownames(results.table), results.table),
          file = "test.csv", row.names = FALSE)