string split on last comma in R

前端 未结 2 806
独厮守ぢ
独厮守ぢ 2020-12-06 06:10

I\'m not new to R but I am relative new to regular expression.

A similar question can be found in here.

An example is if I use

> strsplit(         


        
2条回答
  •  执笔经年
    2020-12-06 06:59

    You can use stri_split function from stringi package

    x <- "USA,UK,Poland"
    stri_split_fixed(x,",") # standard split by comma
    [[1]]
    [1] "USA"    "UK"     "Poland"
    
    stri_split_fixed(x,",",n = 2) # set the max number of elements
    [[1]]
    [1] "USA"       "UK,Poland"
    

    Unfortunately there is no parameter to change the starting point for splitting (from begin/end) but we can handle this another way - using stri_reverse

    stri_split_fixed(stri_reverse(x),",",n = 2) #reverse
    [[1]]
    [1] "dnaloP" "KU,ASU"
    
    stri_reverse(stri_split_fixed(stri_reverse(x),",",n = 2)[[1]]) #reverse back
    [1] "Poland" "USA,UK"
    stri_reverse(stri_split_fixed(stri_reverse(x),",",n = 2)[[1]])[2:1] #and again :)
    [1] "USA,UK" "Poland"
    

提交回复
热议问题