And FWIW, if you've fixed the type problem, there is a function for summing columns called colSums().
apply function error
I have this dataframe and when i'm trying to count sum of columns I'm getting this error message:
Error in FUN(X[[i]], ...) : invalid 'type' (character) of argument.
• 3,754 views
•
link
2 answers
It's telling you that one or more of your columns is formatted as a character instead of numeric, so you need to convert them first.
data[] <- lapply(data, as.numeric)
If you're curious which columns are characters.
colnames(data[, sapply(data, is, "character")])
• 0 views
•
link
• 0 views
•
link
the error invalid 'type' (character) is telling you that the information in the cells of your data frame are characters (i.e. text) not numbers (aka numerics). R can be picky about the "type" and in this case since you can't sum letters you get this error.
First convert your data.frame columns to numerics, one option is:
df <- apply(df, 2, function(x) as.numeric(x))
sums <- apply(df, 2, sum)
though personally I tend to go with colSums for this sort of operation.
• 0 views
•
link
Log in to answer this question.
try this: