5
I have a column in a data.frame that is similar to this structure:
d <- structure(list(value = c(" 2019s/v282930ahead of print ",
" 2018s/v252627 ", " 2017s/v222324 ",
" 2016s/v192021 ", " 2015s/v161718 "
)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"
))
I would like to turn it into 4 other columns with the functions mutate
and str_extract
, if possible (although other suggestions are welcome).
The columns would be:
Year - 4 first digits
Number_1 - 2 digits after string "s/v"
Number_2 - 2 digits after Number_1
Number_3 - 2 digits after Number_2
So as a result for the first row of the new columns would be
Ano Number_1 Number_2 Number_3
2019 28 29 30
What I’m trying to do is this:
library(dplyr)
library(stringr)
d %>%
mutate(value = str_trim(value),
year = str_extract(value, "\\d{4}"),
Number_1 = str_extract(value, "(s/v)\\d{2}"))
# Number_2 = str_extract(value, "(s/v)\\d{2}) - Não sei
# Number_3 = str_extract(value, "(s/v)\\d{2}) - Não sei
Could someone give some tips?
perfect Tomas! Thank you very much! Taking advantage of the line of his excellent explanation, have some material to indicate (preferably in Portuguese) to study regex applied to R? I’m seeing some things, but I hadn’t seen the " look Behind". Anyway, it was super worth it!
– r_rabbit
Regex in [tag:R] is kind of like regex in [tag:perl]. I recommend playing on regex101.com
– Tomás Barcellos
Has this material here too
– Tomás Barcellos
I’ll take a look, thanks!
– r_rabbit