Reputation: 401
Assume a datatable DT below
DT <- data.table(m=1:3, n=factor(c("a","b","c")))
I would like to change the levels of the 2nd column to c("d","e","f"). I can do it using
levels(DT$n) <- c("d","e","f")
But, what should I do if I just know the column index; i.e. 2. I tried the following line but it doesn't work!
i=2
levels(DT[, ..i]) <- c("d","e","f")
This is the error I get:
Error in `[<-.data.table`(`*tmp*`, , ..i, value = list(n = 1:3)) : object '..i' not found
Upvotes: 0
Views: 133
Reputation: 2318
You could still do data.frame
way:
levels(DT[[2]]) <- c("d", "e", "f")
Note however it is usually not recommended to update by column index though..
Upvotes: 1