Is there an alternative to “revalue” function from plyr when using dplyr?

后端 未结 4 1632
再見小時候
再見小時候 2021-02-18 15:57

I\'m a fan of the revalue function is plyr for substituting strings. It\'s simple and easy to remember.

However, I\'ve migrated new code to

4条回答
  •  眼角桃花
    2021-02-18 16:55

    I wanted to comment on the answer by @aosmith, but lack reputation. It seems that nowadays the default of dplyr's recode function is to leave unspecified levels unaffected.

    x = sample(c("a", "b", "c"), 10, replace = TRUE)
    x
    [1] "c" "c" "b" "b" "a" "b" "c" "c" "c" "b"
    
    recode(x , a = "apple", b = "banana" )
    
    [1] "c"      "c"      "banana" "banana" "apple"  "banana" "c"      "c"      "c"      "banana"
    

    To change all nonspecified levels to NA, the argument .default = NA_character_ should be included.

    recode(x, a = "apple", b = "banana", .default = NA_character_)
    
    [1] "apple"  "banana" "apple"  "banana" "banana" "apple"  NA       NA       NA       "apple" 
    

提交回复
热议问题