简体   繁体   中英

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])

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM