简体   繁体   中英

Assign Column name to first column R table

I want to save the following table out of 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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