Search code examples
rstringdataframestrsplit

R: split a string in a dataframe column


I have a dataframe in which one of the columns contains strings. I'd like to split that strings, separated by a dot, and keep always the first part.

This would be my dataframe:

                                             State
1             This is my string. I do not want this
2   This is other string. I do not want this either

I'd like to obtain this:

                   State
1      This is my string
2   This is other string

I've tried with this but it's now working:

df = df >%> dplyr::mutate(State= str_split(State,".")[1])

Solution

  • Does this work:

    library(dplyr)
    library(stringr)
    df
                                                State
    1           This is my string. I do not want this
    2 This is other string. I do not want this either
    df %>% mutate(State = str_remove(State, '\\..*'))
                     State
    1    This is my string
    2 This is other string