Group together levels with similar names R

后端 未结 2 1840
既然无缘
既然无缘 2020-12-19 15:23

I have a variable q with various levels. Some of the levels are actually the same but have been bad reported.

 length(q)
[1] 13490
> levels(q)
  [1] \"         


        
2条回答
  •  执念已碎
    2020-12-19 15:49

    One solution could be to use grep and/or grepl:

    x <- c("toto", "CERACETT","CERASETTE","Cerazette","CERAZETTE","CEVAZETTE", "youpi")
    grep("ce[vr]a[z]ett[e]", x, ignore.case = TRUE, value = TRUE)
    x[grepl("ce[vr]a[sz]ett[e]", x, ignore.case = TRUE)] <- "replacement_string"
    

提交回复
热议问题