r - Using gsub() in a datatable -



r - Using gsub() in a datatable -

i have big info table (about 20,000 rows). 1 of columns contains in integers 1 6.

i have character vector of auto models (6 models).

i'm trying replace integers corresponding auto model.(just 2 in example)

gsub("1",paste0(labels[1]),models) gsub("2",paste0(labels[2]),models) ...

"models" name of column.

labels <- c("altima","maxima")

after fighting 12+ hours gsub() isn't working(

sample data: mydata<-data.table(replicate(1,sample(1:6,10000,rep=true))) labels<-c("altima","maxima","sentra","is","gs","ls")

i don't think need gsub here. describing factor variable.

if info is

mydata <- data.table(replicate(1,sample(1:6,1000,rep=true))) models <- c("altima","maxima","sentra","is","gs","ls")

you do

mydata[[1]] <- factor(mydata[[1]], levels=seq_along(models), labels=models)

if wanted character rather factor, then

mydata[[1]] <- models[ mydata[[1]] ]

would trick. both of these require numbers continuous , start @ 1.

r

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -