Find and replace entire value in R

前端 未结 4 994
青春惊慌失措
青春惊慌失措 2021-01-24 04:29

I\'m looking for a way to use the find and replace function in R to replace the entire value of a string, rather than just the matching part of the string. I have a dataset with

4条回答
  •  忘掉有多难
    2021-01-24 05:25

    gsub() is used to substitute a particular string with another string. In the above code, if you do the following, your whole string changes to exp

    result <- gsub(string, "exp", string)
    

    But, if you use grep() and replace(), you will achieve your desired result.

    res1 <- grep("pattern",string)
    

    gives you all the lines with the pattern and use this in replace().

    res_new <- replace(string,res1,"exp")
    

提交回复
热议问题