extract string from pattern but exclude first character in the match

How can we extract the pattern but exclude the first character.

An example from here
https://rstudio-pubs-static.s3.amazonaws.com/347744_c25378610af54ff5a9b5fc0f0bd9b587.html#parsing-strings-into-variables

date_ranges <- c("23.01.2017 - 29.01.2017", "30.01.2017 - 06.02.2017")

# Split dates using " - "
split_dates <- str_split(date_ranges, pattern = fixed(" - "))

# Print split_dates
split_dates

instead of this

## [[1]]
## [1] "23.01.2017" "29.01.2017"
## 
## [[2]]
## [1] "30.01.2017" "06.02.2017"

expected would be a list consisting
Second string but excluding the first character i.e

9.01.2017
6.02.2017

because if I use another mutate to do this, it is giving an error like below:
Error: Column split_dates must be a 1d atomic vector or a list

You can use something like this:

split_dates <- lapply(str_split(date_ranges, pattern = fixed(" - ")), "[[" , 2 ) %>% 
  lapply(., function(x) substr(x, start = 2, stop = nchar(x)))
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.