replace a specific strings from multiple columns in a dataframe

前端 未结 4 1555
失恋的感觉
失恋的感觉 2021-01-27 17:49

I want to replace the strings \"aa\" and \"aaa\" in the dataframe below with\"\"

data = data.frame(attr = c(1:4), type1=c(         


        
4条回答
  •  天命终不由人
    2021-01-27 18:30

    library(dplyr)
    data2 = data %>%
      mutate(type1 = recode(type1, "aa" = "", "aaa" = ""),
             type2 = recode(type2, "aa" = "", "aaa" = ""))
    
    attr type1 type2
    1    1            
    2    2     b      
    3    3            
    4    4     b      
    

    if you wanna generalize, taking from @denis answer:

    data2 = data %>%
      mutate_all(function(x) gsub("aa|aaa","",x))
    

提交回复
热议问题