【问题标题】:R replaced values show up in table() [duplicate]R替换值显示在表()中[重复]
【发布时间】:2014-09-27 11:25:33
【问题描述】:

有没有办法让替换值不显示在 table() 函数中?

问题很容易重现 在 R-Studio 版本 0.98.1062、R 版本 3.1.1 下

x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
y <- c("condition1", "condition2", "condition3", "condition1", "condition2", "condition3", "condition1", "condition2", "condition3", "condition1")
df <- data.frame(x, y)

假设我将“condition3”的名称替换为“condition2”

df$y <- replace(df$y, df$y=="condition3","condition2")
table(df$y)

输出 =

条件1 条件2 条件3

4.......................6.......................0

为什么它在表中打印 Condition3,当它有 0 值时? 我想要一个替换 condition3 的函数,这样它就不存在了,不仅在数据框中,而且在任何进一步的分析中。有没有更好的方法来替换值?

【问题讨论】:

标签: r replace


【解决方案1】:

你可以使用droplevels,像这样:

table(droplevels(df$y))
# 
# condition1 condition2 
#          4          6 

【讨论】:

【解决方案2】:

另外,要从 df$y 的级别中永久删除“condition3”,

df$y <- factor(df$y)  

【讨论】:

    猜你喜欢
    • 2019-10-11
    • 1970-01-01
    • 2014-11-03
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 2018-01-05
    • 1970-01-01
    相关资源
    最近更新 更多