An economist
An economist

Reputation: 1311

Extracting pattern from the nested list in R using regex

I have following sorted list (lst) of time periods and I want to split the periods into specific dates and then extract maximum time period without altering order of the list.

$`1`
[1] "01.12.2015 - 21.12.2015"

$`2`
[1] "22.12.2015 - 05.01.2016"

$`3`
[1] "14.09.2015 - 12.10.2015" "29.09.2015 - 26.10.2015"

Therefore, after adjustment list should look like this:

$`1`
[1] "01.12.2015" "21.12.2015"

$`2`
[1] "22.12.2015"  "05.01.2016" 

$`3`
[1] "14.09.2015"  "12.10.2015" "29.09.2015"  "26.10.2015"

In order to do so, I began with splitting the list:

   lst_split <- str_split(lst, pattern = " - ")

which leads to the following:

[[1]]
[1] "01.12.2015" "21.12.2015"

[[2]]
[1] "22.12.2015" "05.01.2016"

[[3]]
[1] "c(\"14.09.2015"             "12.10.2015\", \"29.09.2015" "26.10.2015\")"  

Then, I tried to extract the pattern:

lapply(lst_split, function(x) str_extract(pattern = c("\\d+\\.\\d+\\.\\d+"),x))

but my output is missing one date (29.09.2015)

[[1]]
[1] "01.12.2015" "21.12.2015"

[[2]]
[1] "22.12.2015" "05.01.2016"

[[3]]
[1] "14.09.2015" "12.10.2015" "26.10.2015"

Does anyone have an idea how I could make it work and maybe propose more efficient solution? Thank you in advance.

Upvotes: 0

Views: 203

Answers (2)

G. Grothendieck
G. Grothendieck

Reputation: 269852

1) Use strsplit, flatten each component using unlist, convert the dates to "Date" class and then use range to get the maximum time span. No packages are used.

> lapply(lst, function(x) range(as.Date(unlist(strsplit(x, " - ")), "%d.%m.%Y")))
$`1`
[1] "2015-12-01" "2015-12-21"

$`2`
[1] "2015-12-22" "2016-01-05"

$`3`
[1] "2015-09-14" "2015-10-26"

2) This variation using a magrittr pipeline also works:

library(magrittr)
lapply(lst, function(x) 
   x %>% 
     strsplit(" - ") %>% 
     unlist %>% 
     as.Date("%d.%m.%Y") %>% 
     range
)

Note: The input lst in reproducible form is:

lst <- structure(list(`1` = "01.12.2015 - 21.12.2015", `2` = "22.12.2015 - 05.01.2016", 
`3` = c("14.09.2015 - 12.10.2015", "29.09.2015 - 26.10.2015"
)), .Names = c("1", "2", "3"))

Upvotes: 1

An economist
An economist

Reputation: 1311

Thanks to comments of @WiktorStribiżew and @akrun it is enough to use str_extract_all.

In this example:

> str_extract_all(lst,"\\d+\\.\\d+\\.\\d+")
[[1]]
[1] "01.12.2015" "21.12.2015"

[[2]]
[1] "22.12.2015" "05.01.2016"

[[3]]
[1] "14.09.2015" "12.10.2015" "29.09.2015" "26.10.2015"

Upvotes: 2

Related Questions