0

Is there a way for replaced values not to show up in the table() function?

Problem is easily reproduceable under R-Studio Version 0.98.1062, R version 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)

Let's say I replace the name of "condition3" to "condition2"

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

ouput =

condition1 condition2 condition3

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

Why does it print Condition3 in the table, when it has 0 values? I would like a function that replaces condition3, so that it is not there, not just in the dataframe, but also in any further analysis. Is there a better way to replace values?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Timo Kvamme
  • 2,806
  • 1
  • 19
  • 24

2 Answers2

2

You can use droplevels, like this:

table(droplevels(df$y))
# 
# condition1 condition2 
#          4          6 
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • @user2673238 If this answer is perfect for you, it's considered good pratice to accept the answer. See also this help page: [What should I do when someone answers my question?](http://stackoverflow.com/help/someone-answers) – Jaap Sep 28 '14 at 11:32
0

Also,to permanently remove "condition3" from the levels of df$y,

df$y <- factor(df$y)  
branch.lizard
  • 595
  • 1
  • 5
  • 15