statistics with R: how to avoid the “sum not meaningful for factors” error

if you’re trying to calculate weighted means, create a weighted boxplot, or do something similar in R, and you get an error saying “sum not meaningful for factors”, it means that one of your data columns that should be a vector of numbers, is actually of the data type “factor”.
the data type can easily be checked in R using commands such as is.numeric(x), is.vector(x), is.factor(x) etc.

for me, the problem was a product of different decimal symbols, “.” being R’s standard, and “,” being the decimal character in austria as well as lots of other countries. when i imported the data, decimal numbers were falsely classified as text-strings, and thus the data type of the entire column was set to “factor”.

in order to avoid this problem, you can either change the locale of your data source (for example: “extras – options – international” in microsoft excel) to use the dot (“.”) as the decimal point, or use ...dec = ","... in the import command in R to specify your the decimal character of your input data, such as this:

    dataset <- read.table("clipboard", header = TRUE, dec=",")

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.