Home > database >  Need to get two different numbers in the same string
Need to get two different numbers in the same string

Time:03-17

I have a dataset with this data:

enter image description here

From the Rslt column, I need to extract the first and second numbers, each of which can be either one or two digits, and put each into a new, separate column, the first number into Runs1 and the second number into Runs2.

Sample Output

enter image description here

Code:

I have tried suggested solutions such as this, but it did not work:

ms2 |>
  mutate(runs = stri_extract_all(Rslt, regex="\\d ")[[1]])

This also did not work when I tried to get just the first number:

    ms2 |>
      mutate(R1st = str_extract(Rslt,"^.*(\\d )"))

This one put <chr [2]> in the runs1 column:

ms %>%
  mutate(runs1 = str_split(Rslt, "-"))

I would prefer a dplyr solution; however, I open to other ways of doing it. Also, if there is a stackoverflow solution that does solve my problem, I would appreciate it if you can share its link.

dput:

structure(list(Date = structure(c(1399161600, 1399334400, 1399507200, 
1399766400, 1400025600), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), Tm = c("TOR", "TOR", "TOR", "TOR", "TOR"), Opp = c("PIT", 
"PHI", "PHI", "LAA", "CLE"), Rslt = c("W 7-2", "W 6-5", "W 12-6", 
"L 3-9", "L 4-15"), AppDec = c("8-8", "9-10 W", "7-8", "5-6", 
"7-8")), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"
))

CodePudding user response:

You can also use tidyrs function extract:

library(tidyr)
df %>%
  extract(Rslt,
          into = c("Runs1", "Runs2"),
          regex = "(\\d )-(\\d )", 
          remove = FALSE)
# A tibble: 5 × 7
  Date                Tm    Opp   Rslt   Runs1 Runs2 AppDec
  <dttm>              <chr> <chr> <chr>  <chr> <chr> <chr> 
1 2014-05-04 00:00:00 TOR   PIT   W 7-2  7     2     8-8   
2 2014-05-06 00:00:00 TOR   PHI   W 6-5  6     5     9-10 W
3 2014-05-08 00:00:00 TOR   PHI   W 12-6 12    6     7-8   
4 2014-05-11 00:00:00 TOR   LAA   L 3-9  3     9     5-6   
5 2014-05-14 00:00:00 TOR   CLE   L 4-15 4     15    7-8 

CodePudding user response:

You can use tidyr::separate for this, eg:

library(tidyverse)
dat %>% separate(col=Rslt, into = c("result", "Runs1", "Runs2"), sep = "[ -]", remove = FALSE )

  Date                Tm    Opp   Rslt   result Runs1 Runs2 AppDec
  <dttm>              <chr> <chr> <chr>  <chr>  <chr> <chr> <chr> 
1 2014-05-04 00:00:00 TOR   PIT   W 7-2  W      7     2     8-8   
2 2014-05-06 00:00:00 TOR   PHI   W 6-5  W      6     5     9-10 W
3 2014-05-08 00:00:00 TOR   PHI   W 12-6 W      12    6     7-8   
4 2014-05-11 00:00:00 TOR   LAA   L 3-9  L      3     9     5-6   
5 2014-05-14 00:00:00 TOR   CLE   L 4-15 L      4     15    7-8   
  • Related