gsub return an empty string when no match is found

后端 未结 7 1598
太阳男子
太阳男子 2021-01-12 04:35

I\'m using the gsub function in R to return occurrences of my pattern (reference numbers) on a list of text. This works great unless no match is found, in whic

相关标签:
7条回答
  • 2021-01-12 05:19

    I'd probably go a different route, since the sapply doesn't seem necessary to me as these functions are vectorized already:

    fun <- function(x){
        ind <- grep(".*(Ref. (\\d+)).*",x,value = FALSE)
        x <- gsub(".*(Ref. (\\d+)).*", "\\1", x)
        x[-ind] <- ""
        x
    }
    
    fun(data)
    
    0 讨论(0)
提交回复
热议问题