【问题标题】:R: split a string in a dataframe columnR:在数据框列中拆分字符串
【发布时间】:2021-05-11 15:22:09
【问题描述】:

我有一个数据框,其中一列包含字符串。我想拆分这些字符串,用点分隔,并始终保留第一部分。

这将是我的数据框:

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

【问题讨论】:

    标签: r string dataframe strsplit


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2018-01-22
      • 2020-08-07
      • 2017-06-12
      • 2018-08-27
      • 2021-02-27
      • 2012-09-22
      • 1970-01-01
      • 2022-11-20
      • 2022-01-22
      相关资源
      最近更新 更多