Change an integer into a specific string in a data.frame

后端 未结 3 1000
深忆病人
深忆病人 2021-01-02 07:02

I have a data frame with two columns. The second column contains only integers. More precisely it contains 0,1,2,3 and some NA\'s. Something like this:

id1           


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-02 08:04

    You can map values using the mapvalues function from the plyr package. Using the example data from Mike Wise's answer:

    library(plyr)
    df$val2 <- mapvalues(df$val,
                               from = c(0,1,2,3,NA),
                               to = c("ZZT", "ZZU", "ZZV", "ZZW", NA))
    

    If you already have the dplyr package loaded (the successor to plyr), call this function usingplyr::mapvalues() as loading plyr on top of dplyr is problematic.

提交回复
热议问题