How to Fix the R error in oldclass(stats) = cl : adding class ‘factor’ to an invalid object

The “error in oldclass(stats) = cl : adding class ‘factor’ to an invalid object” error message occurs when you try to add a variable that is a class factor to a boxplot by itself. This is because the boxplot function only plots numeric values. Fortunately, there are two ways that you can fix this problem.

Description of the error

This error message occurs because the boxplot function only plots numeric values and not factors. It will however use factors as labels on your boxplot. You can fix this problem one of two ways. The first is to convert the factor into a numeric variable so that it can be graphed. The second approach involves defining the factor as a label on your boxplot. Both methods provide useful information, the one that you choose depends upon what you are looking for. If you want to graph each of the factors then the second option is the one that you want. If you want to graph the factors themselves, then the first one is the option you want.

Explanation of the error

Here we have two code examples that produce this error message. The first one is a generic example, while the second supplies a more practical example.

> df = data.frame(X = c(7, 4, 5, 2, 0, 9, 3),
+ Y = c(“A”, “B”, “B”, “A”, “C”, “B”, “C”))
> boxplot(df$X)
> boxplot(df$Y)
Error in oldClass(stats) = cl :
adding class “factor” to an invalid object

In this example, you will note that the factor “Y” consists entirely of individual characters. If you use it as an argument in the boxplot function you will get our error message.

> t = as.numeric(Sys.time())
> set.seed(t)
> Food = as.integer(abs(rnorm(7))*10)
> Pets = c(“cat”, “dog”, “faret”, “mouse”, “cat”, “dog”, “dog”)
> df = data.frame(Food, Pets)
> boxplot(df$Food)
> boxplot(df$Pets)
Error in oldClass(stats) = cl :
adding class “factor” to an invalid object

In this example, you will note that the factor “Pets” consists entirely of animal names. If you use it as an argument in the boxplot function, you will get our error message. As you can see the boxplot function will not accept non-numeric values.

How to fix the error

Here we have two examples of each of the two ways to fix this problem.

> df = data.frame(X = c(7, 4, 5, 2, 0, 9, 3),
+ Y = c(“A”, “B”, “B”, “A”, “C”, “B”, “C”))
> df$Y = as.numeric(as.factor(df$Y))
> df
X Y
1 7 1
2 4 2
3 5 2
4 2 1
5 0 3
6 9 2
7 3 3
> boxplot(df$X)
> boxplot(df$Y)

> t = as.numeric(Sys.time())
> set.seed(t)
> Food = as.integer(abs(rnorm(7))*10)
> Pets = c(“cat”, “dog”, “faret”, “mouse”, “cat”, “dog”, “dog”)
> df = data.frame(Food, Pets)
> df$Pets = as.numeric(as.factor(df$Pets))
> df
Food Pets
1 2 1
2 16 2
3 12 3
4 3 4
5 9 1
6 13 2
7 5 2
> boxplot(df$Food)
> boxplot(df$Pets)

If you look at these two examples, you will see that we converted the factor into numeric values so that they can be graphed. This setup produces a boxplot graph for each of the two columns.

> df = data.frame(X = c(7, 4, 5, 2, 0, 9, 3),
+ Y = c(“A”, “B”, “B”, “A”, “C”, “B”, “C”))
> boxplot(df$X~df$Y)

> t = as.numeric(Sys.time())
> set.seed(t)
> Food = as.integer(abs(rnorm(7))*10)
> Pets = c(“cat”, “dog”, “faret”, “mouse”, “cat”, “dog”, “dog”)
> df = data.frame(Food, Pets)
> boxplot(df$Food~df$Pets)

In these two examples, we combine the two columns into a single box plot, where the factors are the labels. Both of these approaches fix the problem, but they do not produce the same results. You need to decide on the method based on the results that you want.

While this can be an easy error message to get. You have two straightforward ways of fixing it. You can change the factor into numerical values, or you can set the factor as labels on a box plot. Decide which one is best for your situation and you will have this problem fixed.

Scroll to top
Privacy Policy