【问题标题】:Replacing text in vector with numbers?用数字替换向量中的文本?
【发布时间】:2017-06-14 08:26:47
【问题描述】:

伙计们。我搜索了,但失败了。我对这些东西完全陌生。 我有一个数据集 - 艺术。 我要去 art$paper 看看我有两种类型的纸 - 水彩和素描。 我需要为这两种类型的纸张创建分布。 我尝试了 a ,然后是 hist(a),但它在 hist 中写入
Error .default(a) : 'x' 必须是数字 我知道论文的类型应该是数字的。我该如何更换它。我的 R 语言不好,所以我的问题很愚蠢。 我需要分别创建水彩纸和绘图纸的分布。 非常感谢。

【问题讨论】:

  • 如果您需要替换文本,请使用hist(as.integer(factor(a$paper)))match,即hist(match(a$paper, unique(a$paper)))

标签: r vector replace


【解决方案1】:

如果art$paper 中有两个变量,它可能是一个因素。直方图仅适用于数值变量。

你可以试试这个:

barplot(prop.table(table(art$paper)))

或者使用ggplot2

library(ggplot2)
ggplot <- ggplot(art,aes(x=paper))
ggplot + geom_histogram(fill="black") # numbers
ggplot + geom_histogram(fill="red",aes(y=..count../sum(..count..))) # histogram

【讨论】:

    猜你喜欢
    • 2020-12-07
    • 2015-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 2014-10-29
    • 2017-12-26
    相关资源
    最近更新 更多