3

I want to change a couple of data.table columns from factor to character

library(data.table)

ir <- as.data.table(iris)
ir[, Species2 := Species]

I can identify which columns I need to change

facs <- which(sapply(ir, is.factor))
facs

And I can update the columns by name:

ir[, c("Species", "Species2") := lapply(.SD, as.character), .SDcols = facs]
sapply(ir, class)

Is there a way to update the columns without referencing them by name?

Ed G
  • 802
  • 1
  • 6
  • 19
  • Here, you changed the class by finding which columns are factor. I didn't understand how you want the result – akrun May 14 '15 at 15:34
  • I've got the result I want, but it relies on me knowing the names of the columns which are factors when I use `:=`. Is there a programatic way to replace `c("Species", "Species2")` – Ed G May 14 '15 at 15:41
  • You can use the numeric index `5:6 := ` – akrun May 14 '15 at 15:43
  • Parentheses work here `ir[,(facs):=lapply(.SD,as.character),.SDcols=facs]`. Also, `set`. – Frank May 14 '15 at 15:46
  • Perfect - data.table gets better and better! – Ed G May 14 '15 at 15:48
  • Possible duplicate of [Elegantly assigning multiple columns in data.table with lapply()](http://stackoverflow.com/questions/16943939/elegantly-assigning-multiple-columns-in-data-table-with-lapply) – Frank May 14 '15 at 15:48

1 Answers1

4

You're very close. As @akrun mentioned in a comment, you can reference the columns by index, which you've obtained using which.

ir[, which(sapply(ir, is.factor)) := lapply(.SD, as.character), .SDcols = facs]

Or even better, as @Frank mentioned in a comment, you can use parentheses.

ir[, (fac) := lapply(.SD, as.character), .SDcols = fac]

Now if you look at str(ir), you'll see that Species and Species2 are now chr rather than factor.

Alex A.
  • 5,466
  • 4
  • 26
  • 56