# Fixing R Error Message: not meaningful for factors in r

Error messages are an unfortunate part of programming, but getting them also means that you are writing code. Some of these do not make sense at first, but that changes once you understand it. The “not meaningful for factors” warning message is an example of this type. At first glance, it makes no sense, but once you understand what is going on, it is easy to understand.

## The circumstances of this error.

This warning message occurs when you are using the factor() function. A factor is a data structure designed for data fields that have a finite number of preset values.

> y = factor(c(1,2,3,4,5,6,7))
> x = y[5]
> x
[1] 5
Levels: 1 2 3 4 5 6 7
> y[1] > y[5]
[1] NA
Warning message:
In Ops.factor(y[1], y[5]) : ‘>’ not meaningful for factors

The meaning of this message is simple, and that is that the greater than sign has no meaning when dealing with an unordered factor data structure. This is because in such data structures there is no order for one to be greater than the other.

## What is causing this error?

What is causing this error is that an unordered factor data structure has no specific order to it and so a greater than or less than sign has no meaning in such a data structure.

> y = factor(c(1,2,3,4,5,6,7), ordered = FALSE)
> y[1] > y[5]
[1] NA
Warning message:
In Ops.factor(y[1], y[5]) : ‘>’ not meaningful for factors

In this case, the factor() includes the argument “ordered = FALSE” which is the default value of this argument. Then, if you try to use a greater than or less than sign we will get our warning message.

> y = factor(c(1,2,3,4,5,6,7), ordered = TRUE)
> y[1] > y[5]
[1] FALSE

In this case, the factor() function includes the argument “ordered = TRUE” and you try to use a greater than or less than sign we will not get our warning message. This occurs because your factor is ordered such that greater than and less than produce meaningful results.

## How to fix this error.

There are two main approaches fixing this error and they are both quite simple. Either one will eliminate the error, but which one is best depends on what you were trying to do with your program.

The first fix is to not use a greater than or less than sign with a factor data structure. If you obey this rule, you’ll never get this warning message.

The second way to fix this error is to use the argument “ordered = TRUE” in the factor() function. When you do this, the greater than and equal signs will work and you will not get this warning message.

Scroll to top