简体   繁体   English

将列名分配给第一列 R 表

[英]Assign Column name to first column R table

I want to save the following table out of R :我想从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=",")

However, I am unable to provide a column name for the rownames:但是,我无法为行名提供列名:

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

UPDATE: ANSWER更新:答案

According to a comment, this is impossible.根据评论,这是不可能的。 Here is a roundabout way of doing this, if you are interested:如果您有兴趣,这是一种迂回的方法:

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)

A bit shorter than what you suggested in your question's "UPDATE" would be:比您在问题的“更新”中建议的要短一点的是:

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

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

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