user1320502
user1320502

Reputation: 2570

Remove all characters before a period in a string

This keeps everything before a period:

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

how to keep everything after the period?

Upvotes: 16

Views: 22477

Answers (5)

JDie
JDie

Reputation: 761

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"

Upvotes: 0

Tyler Rinker
Tyler Rinker

Reputation: 109874

If you don't want to think about the regex for this the qdap package has the char2end function that grabs from a particular character until the end of the string.

data <- c("foo.bar", "foo.bar.barfoo")

library(qdap)
char2end(data, ".")

## [1] "bar"        "bar.barfoo"

Upvotes: 2

akrun
akrun

Reputation: 887148

You could use stringi with lookbehind regex

 library(stringi)
 stri_extract_first_regex(data1, "(?<=\\.).*")
 #[1] "bar.barfoo"
 stri_extract_first_regex(data, "(?<=\\.).*")
 #[1] "barfoo"

If the string doesn't have ., this retuns NA (it is not clear about how to deal with this in the question)

 stri_extract_first_regex(data2, "(?<=\\.).*")
 #[1] NA

###data
data <- 'foobar.barfoo' 
data1 <- 'foo.bar.barfoo'
data2 <- "foobar"

Upvotes: 5

Avinash Raj
Avinash Raj

Reputation: 174706

To remove all the characters before a period in a string(including period).

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

Example:

> data <- 'foobar.barfoo'
> gsub("^.*\\.","", data)
[1] "barfoo"

To remove all the characters before the first period(including period).

> data <- 'foo.bar.barfoo'
> gsub("^.*?\\.","", data)
[1] "bar.barfoo"

Upvotes: 30

aelor
aelor

Reputation: 11116

use this :

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

this will keep everything after period

Upvotes: 1

Related Questions