0

I am trying to convert all columns of my data.table to class factor. However, I get the following error message:

Error in sort.list(y) : 'x' must be atomic for 'sort.list'. Have you called 'sort' on a list?

The code I used is as follows:

colnms = colnames(dt)
dt[,colnms:=lapply(.SD,as.factor),.SDcols=colnms]
tushaR
  • 3,083
  • 1
  • 20
  • 33
  • I think the error message was pretty helpful here -- always try and read it carefully! As for making it actionable, if I see this, my first thought is to check the column types of `dt`: either `print(dt, class=TRUE)`, or `sapply(dt, class)` – MichaelChirico Jan 04 '20 at 14:08

1 Answers1

0

When you have multiple columns to assign use round brackets around column names. Try :

library(data.table)
dt[,(colnms) := lapply(.SD,as.factor), .SDcols=colnms]

Since you want to convert all the columns this will also work without specifying .SDcols

dt[,(colnms) := lapply(.SD,as.factor)]

Using a reproducble example :

dt <- mtcars
setDT(dt)
colnms = colnames(dt)
dt[,(colnms) := lapply(.SD,as.factor), .SDcols=colnms]
str(dt)

#Classes ‘data.table’ and 'data.frame': 32 obs. of  11 variables:
# $ mpg : Factor w/ 25 levels "10.4","13.3",..: 16 16 19 17 13 12 3 20 19 14 ...
# $ cyl : Factor w/ 3 levels "4","6","8": 2 2 1 2 3 2 3 1 1 2 ...
# $ disp: Factor w/ 27 levels "71.1","75.7",..: 13 13 6 16 23 15 23 12 10 14 ...
# $ hp  : Factor w/ 22 levels "52","62","65",..: 11 11 6 11 15 9 20 2 7 13 ...
# $ drat: Factor w/ 22 levels "2.76","2.93",..: 16 16 15 5 6 1 7 11 17 17 ...
# $ wt  : Factor w/ 29 levels "1.513","1.615",..: 9 12 7 16 18 19 21 15 13 18 ...
# $ qsec: Factor w/ 30 levels "14.5","14.6",..: 6 10 22 24 10 29 5 27 30 19 ...
# $ vs  : Factor w/ 2 levels "0","1": 1 1 2 2 1 2 1 2 2 2 ...
# $ am  : Factor w/ 2 levels "0","1": 2 2 2 1 1 1 1 1 1 1 ...
# $ gear: Factor w/ 3 levels "3","4","5": 2 2 2 1 1 1 1 2 2 2 ...
# $ carb: Factor w/ 6 levels "1","2","3","4",..: 4 4 1 1 2 1 4 2 2 4 ...
# - attr(*, ".internal.selfref")=<externalptr> 
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Strange. Even with the round brackets, I get the same error. Let me check the data if any of the columns have undesired values. – tushaR Jan 04 '20 at 10:17
  • 1
    @tushaR in that case, it would be helpful to have a reproducible example because as shown it works as expected for the sample `mtcars` dataset. – Ronak Shah Jan 04 '20 at 10:22
  • 1
    Thanks for your help. There was a wrong column which I had created by executing this line of code `dt[,colnms:=lapply(.SD,as.factor),.SDcols=colnms]`. – tushaR Jan 04 '20 at 10:36