简体   繁体   English

使用 psych::describe() 在 csv 中打印变量名称

[英]Print variable names in csv using psych::describe()

I'm using R's psych package. When I use describe() as below to get descriptive statistics of variables, their names appeared in the console but do not appear in the exported csv file.我正在使用 R 的 psych package。当我如下使用describe()获取变量的描述性统计信息时,它们的名称出现在控制台中但没有出现在导出的 csv 文件中。
How can I get this column printed, so that I know which results belong to which variable.如何打印此列,以便我知道哪些结果属于哪个变量。

library(psych)
descriptive2 <- describe(mtcars)
write.csv(descriptive2, "temp.csv")

Don't see any issue with your code other than the misspelling of write.csv . 除了write.csv的拼写错误之外,没有看到任何其他代码问题。 The variables are in the row names which are by default printed in the csv file. 变量位于行名称中,默认情况下,该行名称打印在csv文件中。

library(psych)
write.csv(data.frame(describe(mtcars)),"temp.csv")

Alternative will be: 替代方法是:

descriptive2 <- data.frame(describe(mtcars))
descriptive2$vars <- row.names(descriptive2)

Add the argument row.names = TRUE when exporting your csv file.导出 csv 文件时添加参数row.names = TRUE As mentioned @Adam Quek, make sure to correct the function write.csv() .如@Adam Quek 所述,请确保更正 function write.csv()

library(psych)
descriptive2<-describe(mtcars)
write.csv(descriptive2,"temp.csv", row.names = TRUE)

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

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