Remove specific last character from string

前端 未结 2 1536
时光说笑
时光说笑 2021-01-12 03:55

I have following string vector:

 EC02   502R   603           515    602   
 KL07   601    511R   505R   506R   503   
 508    514    501    509R   510    50         


        
相关标签:
2条回答
  • 2021-01-12 04:11
    lapply( dfrm, function(col_) {gsub( "R","",col_)} )
    
    0 讨论(0)
  • 2021-01-12 04:19

    If you have a string vector and want to replace the last R character from it you can use sub. $ here ensures that the R is the last character in your vector.

    sub("R$", "", str)
    
    #[1] "EC02"  "502"   "603"   "5RFRS"
    

    data

    str <- c("EC02", "502R","603", "5RFRS)
    

    I have used sub here instead of gsub. sub replaces only first occurrence of the pattern whereas gsub replaces all occurrences of the pattern though in this case the usage of sub/gsub wouldn't matter.

    0 讨论(0)
提交回复
热议问题