【问题标题】:Make a percentage table with stargazer用 stargazer 制作百分比表
【发布时间】:2018-10-12 09:03:29
【问题描述】:

我有这样的数据,df1:

Product Relative_Value
Car     0.12651458
Plane   0.08888552
Tank    0.03546231
Bike    0.06711630
Train   0.06382191

Relative_Value 是产品总销售额的百分比。我使用stargazer(df1, summary=FALSE, rownames=TRUE) 获取乳胶代码来制作一个漂亮的数据表。但是我如何让 R 或 TeXstudio 理解 Relative_Value 应该格式化为百分比而不是小数?

【问题讨论】:

    标签: r latex percentage stargazer


    【解决方案1】:

    您可以使用 scales 包中的 percent()

    library(scales)
    
    df1 <- df1 %>%
        mutate(Relative_Value = percent(Relative_Value))
    

    注意 percent() 将列类型更改为字符

    【讨论】:

    • 当我在我的 df 上运行它时,我得到 Error in mutate_impl(.data, dots) : Evaluation error: non-numeric argument to mathematical function.
    • Relative_Value 是不是一个非数字列,可能是偶然的?添加 as.numeric() 以确保,如下所示:mutate(Relative_Value = percent(as.numeric(Relative_Value)))
    • 是的,我只是通过将 Relative_Value 更改为字符(从数字)然后将它放入 percent() 来测试它,我得到了和你一样的错误。那是你的问题。
    • 这似乎是问题所在!谢谢:)(在实际代码中,我在底部添加了一行来总结我拥有的每个df的Relative_Value)
    猜你喜欢
    • 1970-01-01
    • 2017-03-11
    • 2014-02-10
    • 2020-04-04
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    相关资源
    最近更新 更多