Sunday, January 13, 2019

R: Not meaningful for factors even though its data type is numeric




I'm trying to filter by a column which has a "numeric" type already, but it keeps telling me its not meaningful to compare it using ">".



enter image description here


Answer



We can convert those factor columns to numeric



library(dplyr)
library(magrittr)

mydata %<>%
mutate_if(is.factor, funs(as.numeric(as.character(.))))





Or using base R



i1 <- sapply(mydata, is.factor)
mydata[i1] <- lapply(mydata[i1], function(x) as.numeric(as.character(x)))



and then the code should work


No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...