Replace entire expression that contains a specific string

后端 未结 2 1103
花落未央
花落未央 2021-01-03 13:06

I have data frame that has a column with large number of file names like:

d <- c(\"harry11_scott80_norm.avi\",\"harry11_norm.avi\",\"harry11_scott80_lpf.         


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-03 13:10

    Here's one way

    > gsub(".*_scott80_.*", "incongruent", d)
    [1] "incongruent"           "harry11_norm.avi"      "incongruent"          
    [4] "joel51_lpf.avi"        "rich82_joel51_lpf.avi"
    

    Or with grep

    > d[grep("_scott80_", d)] <- "incongruent"
    > d
    [1] "incongruent"           "harry11_norm.avi"      "incongruent"          
    [4] "joel51_lpf.avi"        "rich82_joel51_lpf.avi"
    

    To address your edit, I believe this will do it (using | to mean "or")

    gsub(".*(_scott80_|_harry11_).*", "incongruent", d)
    

    Of course, you don't have any strings in d that match "_harry11_"

提交回复
热议问题