首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导出Hmisc::描述输出到excel/csv

导出Hmisc::描述输出到excel/csv
EN

Stack Overflow用户
提问于 2021-05-20 09:16:34
回答 2查看 127关注 0票数 0

有没有什么方法可以将这些数据导出到csv文件,而不是手动键入内容。以下是Hmisc describe函数的输出:

代码语言:javascript
复制
library(Hmisc) # Hmisc describe
> Hmisc::describe(data)
data 

 3  Variables      6  Observations
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
ID 
       n  missing distinct     Info     Mean      Gmd 
       6        0        3    0.857    112.2    1.267 
                            
Value        110   112   113
Frequency      1     2     3
Proportion 0.167 0.333 0.500
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
Date 
       n  missing distinct 
       6        0        3 
                                           
Value      23/04/2018 24/04/2018 25/04/2018
Frequency           3          2          1
Proportion      0.500      0.333      0.167
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
Revenue 
       n  missing distinct     Info     Mean      Gmd 
       6        0        6        1       74     17.2 

lowest : 51 65 70 85 86, highest: 65 70 85 86 87
                                              
Value         51    65    70    85    86    87
Frequency      1     1     1     1     1     1
Proportion 0.167 0.167 0.167 0.167 0.167 0.167
--------------------------------------------------------------------------------------------------------------------------------------------------------------------

数据集:

代码语言:javascript
复制
> data
   ID       Date Revenue
1 113 23/04/2018      51
2 113 23/04/2018      87
3 113 23/04/2018      70
4 112 24/04/2018      85
5 112 24/04/2018      65
6 110 25/04/2018      86
EN

回答 2

Stack Overflow用户

发布于 2021-05-20 09:40:53

我怀疑将其写入csv是否有帮助。请尝试将其写入文本文件。

代码语言:javascript
复制
cat(capture.output(Hmisc::describe(data)), file = 'result.txt', sep = '\n')
票数 1
EN

Stack Overflow用户

发布于 2021-05-20 09:49:44

这可能并不容易。您可以使用capture.output,但是您需要根据它们的类和计数对这些部分进行不同的解析。您也可以将结果分配给一个数据对象,然后尝试使用该数据对象,但同样,也会有多种格式:

代码语言:javascript
复制
obj <- describe(iris)
str(obj)
#  this is the canonical example of a dataframe but it doesn't even capture all the cases.
List of 5
 $ Sepal.Length:List of 6
  ..$ descript: chr "Sepal.Length"
  ..$ units   : NULL
  ..$ format  : NULL
  ..$ counts  : Named chr [1:13] "150" "0" "35" "0.998" ...
  .. ..- attr(*, "names")= chr [1:13] "n" "missing" "distinct" "Info" ...
  ..$ values  :List of 2
  .. ..$ value    : num [1:35] 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5 5.1 5.2 ...
  .. ..$ frequency: num [1:35(1d)] 1 3 1 4 2 5 6 10 9 4 ...
  ..$ extremes: Named num [1:10] 4.3 4.4 4.5 4.6 4.7 7.3 7.4 7.6 7.7 7.9
  .. ..- attr(*, "names")= chr [1:10] "L1" "L2" "L3" "L4" ...
  ..- attr(*, "class")= chr "describe"
 $ Sepal.Width :List of 6
  ..$ descript: chr "Sepal.Width"
  ..$ units   : NULL
  ..$ format  : NULL
  ..$ counts  : Named chr [1:13] "150" "0" "23" "0.992" ...
  .. ..- attr(*, "names")= chr [1:13] "n" "missing" "distinct" "Info" ...
  ..$ values  :List of 2
  .. ..$ value    : num [1:23] 2 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 ...
  .. ..$ frequency: num [1:23(1d)] 1 3 4 3 8 5 9 14 10 26 ...
  ..$ extremes: Named num [1:10] 2 2.2 2.3 2.4 2.5 3.9 4 4.1 4.2 4.4
  .. ..- attr(*, "names")= chr [1:10] "L1" "L2" "L3" "L4" ...
  ..- attr(*, "class")= chr "describe"
 $ Petal.Length:List of 6
  ..$ descript: chr "Petal.Length"
  ..$ units   : NULL
  ..$ format  : NULL
  ..$ counts  : Named chr [1:13] "150" "0" "43" "0.998" ...
  .. ..- attr(*, "names")= chr [1:13] "n" "missing" "distinct" "Info" ...
  ..$ values  :List of 2
  .. ..$ value    : num [1:43] 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.9 3 ...
  .. ..$ frequency: num [1:43(1d)] 1 1 2 7 13 13 7 4 2 1 ...
  ..$ extremes: Named num [1:10] 1 1.1 1.2 1.3 1.4 6.3 6.4 6.6 6.7 6.9
  .. ..- attr(*, "names")= chr [1:10] "L1" "L2" "L3" "L4" ...
  ..- attr(*, "class")= chr "describe"
 $ Petal.Width :List of 6
  ..$ descript: chr "Petal.Width"
  ..$ units   : NULL
  ..$ format  : NULL
  ..$ counts  : Named chr [1:13] "150" "0" "22" "0.99" ...
  .. ..- attr(*, "names")= chr [1:13] "n" "missing" "distinct" "Info" ...
  ..$ values  :List of 2
  .. ..$ value    : num [1:22] 0.1 0.2 0.3 0.4 0.5 0.6 1 1.1 1.2 1.3 ...
  .. ..$ frequency: num [1:22(1d)] 5 29 7 7 1 1 7 3 5 13 ...
  ..$ extremes: Named num [1:10] 0.1 0.2 0.3 0.4 0.5 2.1 2.2 2.3 2.4 2.5
  .. ..- attr(*, "names")= chr [1:10] "L1" "L2" "L3" "L4" ...
  ..- attr(*, "class")= chr "describe"
 $ Species     :List of 5
  ..$ descript: chr "Species"
  ..$ units   : NULL
  ..$ format  : NULL
  ..$ counts  : Named num [1:3] 150 0 3
  .. ..- attr(*, "names")= chr [1:3] "n" "missing" "distinct"
  ..$ values  :List of 2
  .. ..$ value    : chr [1:3] "setosa" "versicolor" "virginica"
  .. ..$ frequency: num [1:3(1d)] 50 50 50
  ..- attr(*, "class")= chr "describe"
 - attr(*, "descript")= chr "iris"
 - attr(*, "dimensions")= int [1:2] 150 5
 - attr(*, "class")= chr "describe"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67612749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档