【问题标题】:R: Is it possible to split according to various characters with str_split_fixed?R:str_split_fixed可以根据各种字符进行拆分吗?
【发布时间】:2016-10-31 03:07:46
【问题描述】:

我有一个字符串,我想将它分成不同的部分。

test = c("3 CH • P" ,"9 CH • P" , "2 CH • P" , "2 CH, 5 ECH • V",                 
 "3 ECH • V",  "4 ECH • P" )

我知道使用str_split_fixed() from stringr() 可以根据某个字符拆分字符串。例如:

test.1 = str_split_fixed(test, c("•"), 2)
> test.1
     [,1]           [,2]
[1,] "3 CH "        " P"
[2,] "9 CH "        " P"
[3,] "2 CH "        " P"
[4,] "2 CH, 5 ECH " " V"
[5,] "3 ECH "       " V"
[6,] "4 ECH "       " P"

但是,我想知道是否可以设置多个字符(例如,"•"",")来拆分字符串?

【问题讨论】:

    标签: r string split fixed stringr


    【解决方案1】:

    您可以尝试使用gsub 摆脱 的:

    test <- c("3 CH • P" ,"9 CH • P" , 
              "2 CH • P" , "2 CH, 5 ECH • V",                 
              "3 ECH • V",  "4 ECH • P" )
    
    test_sub <- gsub("•", ",", test)
    
    str_split_fixed(test_sub, "[, ]+", n = 5)
    
    #or, use this version with an unfixed length:
    strsplit(test_sub, "[, ]+")
    

    This thread 关于字符串拆分可能有用也可能没用。

    【讨论】:

      猜你喜欢
      • 2016-04-03
      • 2014-12-14
      • 1970-01-01
      • 1970-01-01
      • 2016-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多