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

问题描述 投票:1回答:1

我有一个带有一个变量的数据框,如下所示:

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*  

我无法弄清楚如何做到正确。有小费吗?

r strsplit
1个回答
1
投票

使用正向前瞻我们只能在空间上分开\\s,然后立即用数字(?=\\d)

library(tidyr)
df%>% mutate_if(is.factor,as.character) %>% 
      separate(country,sep = '\\s(?=\\d)', into = c('country','number','2','3','4','5' ))

               country number     2     3      4   5
1              Bolivia   0.16  0.16 4.63* 22.10* 450
2              Germany   0.77 6.06*  0.53 15.35* 630
3 Bosnia & Herzegovina   0.72 6.84*  1.03 21.60* 889
© www.soinside.com 2019 - 2024. All rights reserved.