用数字,字符和特殊符号分割复杂的字符串

时间:2018-09-01 12:05:23

标签: r strsplit

我有一个数据框,其中包含一个如下所示的变量:

rownr country
22    Bolivia 0.16 0.16 4.63* 22.10* 450
23    Mozambique 1.11 19.22* 0.19 12.38* 486
24    Germany 0.77 6.06* 0.53 15.35* 630
25    Bosnia & Herzegovina 0.72 6.84* 1.03 21.60* 889

我想将其分成六个单独的变量,如下所示:

rownr country              number 2      3     4      5
22    Bolivia              0.16   0.16   4.63* 22.10* 450
23    Mozambique           1.11   19.22* 0.19  12.38* 486
24    Germany              0.77   6.06*  0.53  15.35* 630
25    Bosnia & Herzegovina 0.72   6.84*  1.03  21.60* 889

这是我尝试过的:

names(df)[1] <- "Strng"
df <- setDT(df)[, paste0("RA", 1:8) := tstrsplit(Strng, " ", type.convert = TRUE, fixed = TRUE)]
df$country <- gsub("[[:digit:]]","",df$Strng)
df$country <- gsub("[[:punct:]]","",df$country)
df$numbers <- gsub("[[:alpha:]]"," ",df$Strng)


df <- select(df, RA1:RA5)
names(df)[1] <- "country" 
names(df)[2] <- "number" 

df$numberss <- strsplit(df$numbers, split=" ", fixed = FALSE, perl = FALSE, useBytes = FALSE)
df <- setDT(df)[, paste0("RA", 1:5) := tstrsplit(numbers, " ", type.convert = TRUE, fixed = TRUE)]

哪个会导致:

rownr country              number  3           4     5      
22    Bolivia              0.16    0.16        4.63* 22.10* 
23    Mozambique           1.11    19.22*      0.19  12.38* 
24    Germany              0.77    6.06*       0.53  15.35* 
25    Bosnia               &       Herzegovina 0.72  6.84*  

我不知道该怎么做。有提示吗?

1 个答案:

答案 0 :(得分:1)

使用正向向前看,我们只能在空格HashMap后面加上数字\\s

进行分割
(?=\\d)