2016-06-28 81 views
1

我有一个字符串,我想分割各个部分。R:是否可以根据str_split_fixed的不同字符进行分割?

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()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" 

不过,我不知道是否可以设置一个以上的字符(例如说,"•"",")分割字符串?

回答

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上串分裂可能会或可能不会有用。

相关问题