split strings on first and last commas

后端 未结 5 1976
生来不讨喜
生来不讨喜 2021-01-13 16:05

I would like to split strings on the first and last comma. Each string has at least two commas. Below is an example data set and the desired result.

A similar ques

5条回答
  •  醉酒成梦
    2021-01-13 16:32

    Using str_match() from package stringr, and a little help from one of your links,

    > library(stringr)
    > data.frame(str_match(my.data$my.string, "(.+?),(.*),(.+?)$")[,-1], 
                 some.data = my.data$some.data)
    #    X1        X2    X3 some.data
    # 1 123  34,56,78    90        10
    # 2  87     65,43    21        20
    # 3  a4        b6 c8888        30
    # 4  11      bbbb ccccc        40
    # 5  uu     vv,ww    xx        50
    # 6   j k,l,m,n,o     p        60
    

提交回复
热议问题