簡體   English   中英

將列名分配給第一列 R 表

[英]Assign Column name to first column R table

我想從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)

比您在問題的“更新”中建議的要短一點的是:

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