-3

I am trying to separate movies released before 1992 from a column 3 that contains year of release from a big data set. What's the error in the command?

> Movies0<-Movies[Movies[,3]<1992,]
Warning message:
In Ops.factor(Movies[, 3], 1992) : < not meaningful for factors
> 
3
  • 1
    Try Movies[as.numeric(as.character(Movies[,3])) < 1992, ].
    – nrussell
    Aug 27, 2015 at 17:33
  • Seems to me the error is in column class, switch to integers instead of factors
    – erasmortg
    Aug 27, 2015 at 17:33
  • 1
    @nrussell I'm pretty sure that your comment solves the problem and I believe that you should post it as an answer.
    – RHertel
    Aug 27, 2015 at 17:44

1 Answer 1

2

Presumably column 3 of your Movies object is a factor variable, although it may appear to be numeric/integer, and as the warning indicates, the < operator is not defined for factors.


For example,

Df <- data.frame(
  NumericColumn = 1:10,
  FactorColumn = as.factor(1:10)
)
##
R> Df[Df[,2] < 5,]
#NumericColumn FactorColumn
#NA              NA         <NA>
#NA.1            NA         <NA>
#NA.2            NA         <NA>
# ...
#Warning message:
#In Ops.factor(Df[, 2], 5) : ‘<’ not meaningful for factors
##
R> Df[Df[,1] < 5,] # okay
##
R> Df[as.numeric(as.character(Df[,2])) < 5,] # okay

Looking at the source code,

R> Ops.factor
#function (e1, e2) 
#{
#  ok <- switch(.Generic, `==` = , `!=` = TRUE, FALSE)
#  if (!ok) {
#    warning(gettextf("%s not meaningful for factors", sQuote(.Generic)))
#    return(rep.int(NA, max(length(e1), if (!missing(e2)) length(e2))))
#  }
# ...

you can see which operators can be used with factors, and < is not one of them.

Not the answer you're looking for? Browse other questions tagged or ask your own question.