0

In the following data frame:

x <-c(rep (c ("s1", "s2", "s3"),each=5 ))
y <- c(rep(c("a", "b", "c", "d", "e"), 3) )               
z<-c(1:15)    

x_name <- "dimensions"
y_name <- "aspects"
z_name<-"value"
df <- data.frame(x,y,z)
names(df) <- c(x_name,y_name, z_name)

I would like to remove factor levels 'a' and 'b' from 'aspects'. When I try

df1<-subset(df, aspects !="a")

it works ok. However,

df1<-subset(df, aspects != c("a", "b"))

removes not all but just some of 'a' and 'b' and gives a warning :

longer object length is not a multiple of shorter object length.

Can you help me figure out what is wrong? Is there a way to use subset to remove 'a' and 'b' in one go?

Vasile
  • 1,017
  • 2
  • 10
  • 19
  • have a look at `%in%`. here, you're asking R to compare `aspects` to the vector `c("a", "b")`, elementwise (`aspects` must not be of length 2, hence the warning). So what you are looking for is `subset(df, !aspects %in% c("a", "b"))` – Cath Oct 16 '15 at 09:18
  • For fun: `"%out%" <- function(x, table) match(x, table, nomatch = 0) == 0; subset(df, aspects %out% c("a", "b"))`. –  Oct 16 '15 at 09:23
  • David I edited it. I think it is a different question now from what you pointed to. – Vasile Oct 16 '15 at 09:25
  • @Vasile The accepted answer is what you need to follow, as pointed out in CathG's comment. –  Oct 16 '15 at 09:31

0 Answers0