【问题标题】:How to format dplyr output in R into doubles (or other workable format)?如何将 R 中的 dplyr 输出格式化为双精度(或其他可行格式)?
【发布时间】:2018-04-01 03:42:46
【问题描述】:

我如何得到这个结果是数值与小标题。

prop_smoking <- df %>% 
    group_by(Age) %>%
    summarise(ratio = sum(Smoking == 'yes')/n()) 

这给了我这样的输出:

Age     Ratio
18-24  .134
25-35  .144
36-50  .189

我想将每个值添加到其对应的向量中,因为我使用的是重采样方法。但是,如果我按以下方式索引:prop_smoking[1,2] 它会给我一个 tibble 类型。因此,当我将它添加到其对应的向量中并想要像使用向量一样执行循环并减去平均值时,我会得到一个“二进制运算符的非数字参数”。我如何索引或转换这些值以便它们易于使用(即doubles

【问题讨论】:

标签: r vector dplyr tibble


【解决方案1】:

将其强制转换为数据框。

prop_smoking <- as.data.frame(prop_smoking)

【讨论】:

    猜你喜欢
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多