I would like to extract some names of variable using regular expressions. The names I will extract are "r_Year[2007,KineticEnergy]","r_Year[2008,KineticEnergy]","r_Year[2009,KineticEnergy]","r_Year[2010,KineticEnergy]","r_Year[2011,KineticEnergy]".
I tried to the below code, but the error showed "Error in stopifnot(is.character(explicit), is.character(patterns), is.character(complete_pars)) : attempt to apply non-function"
library(bayesplot)
ma <- mcmc_intervals(
bmodel,
# pars = "$r_Year",
regex_pars = 20(07|08|09|10|11)+",KineticEnergy]",
prob = 0.80,
point_est = "mean",
prob_outer = 1,
outer_size = 0.2,
point_size = 3,
)
Thanks in advance,
We have to escape the special characters with \\
and use regex for 2007 like \\d{4}
e.g matches 4 numbers:
your_string <- c("r_Year[2007,KineticEnergy]",
"r_Year[2008,KineticEnergy]",
"r_Year[2009,KineticEnergy]",
"r_Year[2010,KineticEnergy]",
"r_Year[2011,KineticEnergy]")
substring(your_string, regexpr("r_Year\\[\\d{4},KineticEnergy\\]", your_string))
output:
[1] "r_Year[2007,KineticEnergy]"
[2] "r_Year[2008,KineticEnergy]"
[3] "r_Year[2009,KineticEnergy]"
[4] "r_Year[2010,KineticEnergy]"
[5] "r_Year[2011,KineticEnergy]"