data.table在所选列中重新编码

时间:2016-05-05 21:43:23

标签: r data.table

所以我在使用data.table进行挣扎。如何使v1和v3成为数字?

dt = data.table(v1 = c('1','2','3'), v2 = c(1,2,3), v3 = c('1','2','3'))
dt[,c(1,3), with = F] = lapply(dt[,c(1,3), with = F], as.numeric)

1 个答案:

答案 0 :(得分:4)

试试这个:

dt <- data.table(v1 = c('1','2','3'), v2 = c(1,2,3), v3 = c('1','2','3'))
dt[,':='(v1=as.numeric(v1),v3=as.numeric(v3))]
sapply(dt,class)