R分割字符串并保留部分

时间:2019-03-11 03:48:28

标签: r list strsplit

我有一个字符串,其中包含橄榄球比赛的起始阵容(从网络中提取),看起来像这样:

 "Crusaders: 15 David Havili, 14 Seta Tamanivalu, 13 Jack Goodhue, 12 Ryan Crotty, 11 George Bridge, 10 Richie Mo’unga, 9 Bryn Hall, 8 Kieran Read, 7 Matt Todd, 6 Heiden Bedwell-Curtis, 5 Sam Whitelock (c), 4 Scott Barrett, 3 Owen Franks, 2 Codie Taylor, 1 Joe MoodyReplacements: 16 Sam Anderson-Heather, 17 Tim Perry, 18 Michael Alaalatoa, 19 Luke Romano, 20 Pete Samu, 21 Mitchell Drummond, 22 Mitchell Hunt, 23 Braydon Ennor"

我想要的基本上是一个有两列的表,一列是玩家的号码,另一列是玩家的名字。例如

position     name
1            Joe Moody
2            Codie Taylor
3            Owen Franks
4            Scott Barrett
...          ...

所有玩家。

我尝试使用strsplit,除以",",但问题出在第一位:

"Crusaders: 15 David Havili"

数字1和16合并

"1 Joe MoodyReplacements: 16 Sam Anderson-Heather".

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

我同意@HongOoi的评论;最好退后一步并确保以更明智的方式导入数据。就是说,这是事后hacky解决方案。完全不知道这样做的普遍程度。

ss <-  "Crusaders: 15 David Havili, 14 Seta Tamanivalu, 13 Jack Goodhue, 12 Ryan Crotty, 11 George Bridge, 10 Richie Mo’unga, 9 Bryn Hall, 8 Kieran Read, 7 Matt Todd, 6 Heiden Bedwell-Curtis, 5 Sam Whitelock (c), 4 Scott Barrett, 3 Owen Franks, 2 Codie Taylor, 1 Joe MoodyReplacements: 16 Sam Anderson-Heather, 17 Tim Perry, 18 Michael Alaalatoa, 19 Luke Romano, 20 Pete Samu, 21 Mitchell Drummond, 22 Mitchell Hunt, 23 Braydon Ennor"


library(tidyverse)
data.frame(ss = ss) %>%
    mutate(ss = str_replace(ss, "Replacements", "")) %>%   # Remove "Replacements"
    mutate(ss = str_split(ss, "(,|:) ")) %>%               # Split on "," or ":"
    unnest() %>%
    separate(ss, c("position", "name"), sep = "(?<=\\d)\\s", fill = "right") %>%
    filter(!is.na(name))                                   # Remove the first "Crusaders" line
#   position                  name
#1        15          David Havili
#2        14       Seta Tamanivalu
#3        13          Jack Goodhue
#4        12           Ryan Crotty
#5        11         George Bridge
#6        10        Richie Mo’unga
#7         9             Bryn Hall
#8         8           Kieran Read
#9         7             Matt Todd
#10        6 Heiden Bedwell-Curtis
#11        5     Sam Whitelock (c)
#12        4         Scott Barrett
#13        3           Owen Franks
#14        2          Codie Taylor
#15        1             Joe Moody
#16       16  Sam Anderson-Heather
#17       17             Tim Perry
#18       18     Michael Alaalatoa
#19       19           Luke Romano
#20       20             Pete Samu
#21       21     Mitchell Drummond
#22       22         Mitchell Hunt
#23       23         Braydon Ennor

答案 1 :(得分:0)

这是一种适用于示例字符串的快速而肮脏的方法。如果开始时缺少团队名称,则无法在其他字符串上使用。

player.string <- "Crusaders: 15 David Havili, 14 Seta Tamanivalu, 13 Jack Goodhue, 12 Ryan Crotty, 11 George Bridge, 10 Richie Mo’unga, 9 Bryn Hall, 8 Kieran Read, 7 Matt Todd, 6 Heiden Bedwell-Curtis, 5 Sam Whitelock (c), 4 Scott Barrett, 3 Owen Franks, 2 Codie Taylor, 1 Joe MoodyReplacements: 16 Sam Anderson-Heather, 17 Tim Perry, 18 Michael Alaalatoa, 19 Luke Romano, 20 Pete Samu, 21 Mitchell Drummond, 22 Mitchell Hunt, 23 Braydon Ennor"

df <- read.table(text = gsub("(\\d+)", "\\1\t", gsub("Replacements:|(^[^:]*:)|, ", "\n", player.string)), header = FALSE, sep = "\t", col.names = c("Number", "Name")) 
df[order(df$Number),]

   Number                   Name
15      1              Joe Moody
14      2           Codie Taylor
13      3            Owen Franks
12      4          Scott Barrett
11      5      Sam Whitelock (c)
10      6  Heiden Bedwell-Curtis
9       7              Matt Todd
8       8            Kieran Read
7       9              Bryn Hall
...

答案 2 :(得分:0)

使用stringr :: str_match_all()和一些正则表达式,您可以找到并提取所有匹配项,请谨慎使用非贪婪(?)运算符,并在没有逗号的情况下匹配行尾:

library(dplyr)
library(stringr)
ea <- "Crusaders: 15 David Havili, 14 Seta Tamanivalu, 13 Jack Goodhue, 12 Ryan Crotty, 11 George Bridge, 10 Richie Mo’unga, 9 Bryn Hall, 8 Kieran Read, 7 Matt Todd, 6 Heiden Bedwell-Curtis, 5 Sam Whitelock (c), 4 Scott Barrett, 3 Owen Franks, 2 Codie Taylor, 1 Joe MoodyReplacements: 16 Sam Anderson-Heather, 17 Tim Perry, 18 Michael Alaalatoa, 19 Luke Romano, 20 Pete Samu, 21 Mitchell Drummond, 22 Mitchell Hunt, 23 Braydon Ennor"
ea <- unlist(strsplit(ea, "Replacements: "))

tibble(jersey = str_match_all(ea, "\\d+") %>% unlist(),
player = str_match_all(ea, "(?<=\\d\\s).*?(?=.$|,)") %>% unlist())

# A tibble: 23 x 2
   jersey player               
   <chr>  <chr>                
 1 15     David Havili         
 2 14     Seta Tamanivalu      
 3 13     Jack Goodhue         
 4 12     Ryan Crotty          
 5 11     George Bridge