0

I'm learning R and would welcome some explanation/pointers as to what the error means and why it is that I can't simply assign the columns as factors:

In R, I read.csv() a file into a variable DF. class(DF) tells me it's a "data.frame" but columns 1, 2, and 3 are non-factors. When I try to assign columns 1,2,3 as factors I get an error:

asFactors <- c(1:3)
DF[asFactors] <- as.factors(DF[asFactors]) # same if I use DF[,asFactors] 
Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?

What does sort have to do with me trying to change variable type?

Instead, what I seem to need to do is use an apply() function to convert columns to factors (but this isn't the most intuitive thing):

DF[,asFactors] <- lapply(DF[asFactors], factor)

Furthermore, if I try to convert columns to 4,5,6 to numeric using lapply i get a new error:

asNumeric <- c(4:6)
DF[,asNumeric] <- lapply(DF[asNumeric], numeric) 
Error in FUN(X[[i]], ...) : invalid 'length' argument

And if I fall back on my original attempt, I get:

DF[,asNumeric] <- as.numeric(DF[,asNumeric])
Error: (list) object cannot be coerced to type 'double'

So for each variable type I seems= to need a different method of converting columns, or I haven't found the one method that applies to all of them.

Jaap
  • 81,064
  • 34
  • 182
  • 193
val
  • 1,629
  • 1
  • 30
  • 56

1 Answers1

3

To change multiple columns to factor, use:

DF[,1:3] <- lapply(DF[,1:3], factor)

To change from factor to numeric, remember to use as.numeric(as.character(x)), like this:

DF[,1:3] <- lapply(DF[,1:3], function(x) as.numeric(as.character(x)))
www
  • 4,124
  • 1
  • 11
  • 22