R:拆分数据帧列中的字符串
我有一个数据框,其中一列包含字符串。我想把这些字符串分开,用一个点隔开,并始终保留第一部分 这将是我的数据帧:R:拆分数据帧列中的字符串,r,string,dataframe,strsplit,R,String,Dataframe,Strsplit,我有一个数据框,其中一列包含字符串。我想把这些字符串分开,用一个点隔开,并始终保留第一部分 这将是我的数据帧: State 1 This is my string. I do not want this 2 This is other string. I do not want this either 我想得到这个: State 1
State
1 This is my string. I do not want this
2 This is other string. I do not want this either
我想得到这个:
State
1 This is my string
2 This is other string
我已经试过了,但现在可以用了:
df = df >%> dplyr::mutate(State= str_split(State,".")[1])
这是否有效:
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