Replace multiple strings in one gsub() or chartr() statement in R?

前端 未结 6 1534
遇见更好的自我
遇见更好的自我 2020-11-30 07:28

I have a string variable containing alphabet[a-z], space[ ], and apostrophe[\'],eg. x <- \"a\'b c\" I want to replace apostrophe[\'] with blank[], and replac

6条回答
  •  盖世英雄少女心
    2020-11-30 08:04

    I am a fan of the syntax that the %<>% and %>% opperators from the magrittr package provide.

    library(magrittr)
    
    x <- "a'b c"
    
    x %<>%
      gsub("'", "", .) %>%
      gsub(" ", "_", .) 
    x
    ##[1] "ab_c"
    

    gusbfn is wonderful, but I like the chaining %>% allows.

提交回复
热议问题