【问题标题】:summarytools: How to edit variable names or to add text in dfSummary() output?summarytools:如何编辑变量名称或在 dfSummary() 输出中添加文本?
【发布时间】:2019-07-28 02:27:59
【问题描述】:

我需要使用dfSummary() 汇总一个数据集,但我需要替换输出中的变量名称(无需再次重命名整个数据集)。另外,我需要在一些变量上写注释(即哪些变量被颠倒等)

我在文档或在线论坛中都没有找到任何方法。谢谢!

【问题讨论】:

    标签: r summarytools


    【解决方案1】:

    没有简单的方法可以替换变量名。最好重命名数据框本身的变量。有关注释,请参阅label() 函数。

    编辑 1
    您还可以使用包的 print() 函数的 footnote= 参数(如果使用 html 结果)或 caption= 参数用于 ascii / markdown 结果。

    例子:

    print(dfSummary(iris), caption="This is caption text")
    view(dfSummary(iris), footnote="This is <em>footnote</em> text")
    

    编辑 2
    另外,请记住 dfSummary 会生成数据帧。所以创建之后,可以简单的修改一下name列的内容:

    dfs <- dfSummary(iris)
    dfs$Variable
    [1] "Sepal.Length\\\n[numeric]" "Sepal.Width\\\n[numeric]"  "Petal.Length\\\n[numeric]"
    [4] "Petal.Width\\\n[numeric]"  "Species\\\n[factor]"
    dfs$Variable <- <- sub("\\.", " ", dfs$Variable)
    print(dfs, graph.col = FALSE, na.col = FALSE)
    
    Data Frame Summary  
    iris  
    Dimensions: 150 x 5  
    Duplicates: 1  
    
    ---------------------------------------------------------------------------
    No   Variable       Stats / Values          Freqs (% of Valid)   Valid     
    ---- -------------- ----------------------- -------------------- ----------
    1    Sepal Length   Mean (sd) : 5.8 (0.8)   35 distinct values   150       
         [numeric]      min < med < max:                             (100.0%)  
                        4.3 < 5.8 < 7.9                                        
                        IQR (CV) : 1.3 (0.1)                                   
    
    2    Sepal Width    Mean (sd) : 3.1 (0.4)   23 distinct values   150       
         [numeric]      min < med < max:                             (100.0%)  
                        2 < 3 < 4.4                                            
                        IQR (CV) : 0.5 (0.1)                                   
    
    3    Petal Length   Mean (sd) : 3.8 (1.8)   43 distinct values   150       
         [numeric]      min < med < max:                             (100.0%)  
                        1 < 4.3 < 6.9                                          
                        IQR (CV) : 3.5 (0.5)                                   
    
    4    Petal Width    Mean (sd) : 1.2 (0.8)   22 distinct values   150       
         [numeric]      min < med < max:                             (100.0%)  
                        0.1 < 1.3 < 2.5                                        
                        IQR (CV) : 1.5 (0.6)                                   
    
    5    Species        1. setosa               50 (33.3%)           150       
         [factor]       2. versicolor           50 (33.3%)           (100.0%)  
                        3. virginica            50 (33.3%)                     
    ---------------------------------------------------------------------------
    

    【讨论】:

    • 能够替换变量名是一项重要功能。人们经常避免在变量名中使用空格,这对表格中的正常显示没有帮助。感谢您提供有用的软件包。
    • @user39008 不客气。我添加了一个示例,说明如何在这种情况下修改 Variable。如果您愿意,也可以在项目的 GitHub 页面上提交功能请求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 2017-02-18
    相关资源
    最近更新 更多