【问题标题】:export Hmisc::describe output to excel/csv导出 Hmisc::describe 输出到 excel/csv
【发布时间】:2021-05-20 01:16:33
【问题描述】:

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

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
--------------------------------------------------------------------------------------------------------------------------------------------------------------------

数据集:

> 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

【问题讨论】:

    标签: r hmisc


    【解决方案1】:

    我怀疑将其写入 csv 会有所帮助。尝试将其写入文本文件。

    cat(capture.output(Hmisc::describe(data)), file = 'result.txt', sep = '\n')
    

    【讨论】:

      【解决方案2】:

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

      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"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-08
        • 2016-04-19
        • 2014-04-19
        • 2015-01-17
        • 2011-12-30
        • 2010-09-23
        • 2010-11-01
        • 1970-01-01
        相关资源
        最近更新 更多