Remove all characters before a period in a string

前端 未结 5 1685
独厮守ぢ
独厮守ぢ 2020-12-09 18:01

This keeps everything before a period:

gsub(\"\\\\..*\",\"\", data$column )

how to keep everything after the period?

5条回答
  •  孤城傲影
    2020-12-09 18:15

    require(stringr)

    I run a course on Data Analysis and the students came up with this solution :

    get_after_period <- function(my_vector) {    
    
            # Return a string vector without the characters
            # before a period (excluding the period)
    
            # my_vector, a string vector
    
            str_sub(my_vector, str_locate(my_vector, "\\.")[,1]+1) 
    
            }
    

    Now, just call the function :

    my_vector <-  c('foobar.barfoo', 'amazing.point')
    
    get_after_period(my_vector)
    
    [1] "barfoo" "point"
    

提交回复
热议问题