lookup tables - How to change an integer into a specific string in a data.frame in R -


i guess simple question reason i'm stuck on it. have data frame 2 columns. second column contains integers. more precisely contains 0,1,2,3 , na's. this:

id1    0 id2    1 id3    0 id4    2 id5    3 id6    1 id7    2 id8    na 

what i'm searching command changes 0 zzt 1 zzu , on. na's should stay na's. how work?

i tried loop in combination if-statements doesn't work. know such changing thinks pretty easy in r seems have block in brain.

you can map values using mapvalues function plyr package. using example data mike wise's answer:

library(plyr) df$val2 <- mapvalues(df$val,                            = c(0,1,2,3,na),                            = c("zzt", "zzu", "zzv", "zzw", na)) 

if have dplyr package loaded (the successor plyr), call function usingplyr::mapvalues() loading plyr on top of dplyr problematic.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -