Sven
Sven

Reputation: 83

R: Define ranges from text using regex

I need a way to call defined variables dependant from a string within text. Let's say I have five variables (r010, r020, r030, r040, r050). If there is a given text in that form "r010-050" I want to have the sum of values from all five variables.

The whole text would look like "{r010-050} == {r060}" The first part of that equation needs to be replaced by the sum of the five variables and since r060 is also a variable the result (via parsing the text) should be a logical value.

I think regex will help here again. Can anyone help? Thanks.

Upvotes: 0

Views: 78

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269654

Define the inputs: the variables r010 etc. which we assume are scalars and the string s.

Then define a pattern pat which matches the {...} part and a function Sum which accepts the 3 capture groups in pat (i.e. the strings matched to the parts of pat within parentheses) and performs the desired sum.

Use gsubfn to match the pattern, passing the capture groups to Sum and replacing the match with the output of Sum. Then evaluate it.

In the example the only variables in the global environment whose names are between r010 and r050 inclusive are r010 and r020 (it would have used more had they existed) and since they sum to r060 it returned TRUE.

library(gsubfn)

# inputs
r010 <- 1; r020 <- 2; r060 <- 3
s <- "{r010-050} == {r060}"

pat <- "[{](\\w+)(-(\\w+))?[}]"
Sum <- function(x1, x2, x3, env = .GlobalEnv) {
  x3 <- if(x3 == "") x1 else paste0(gsub("\\d", "", x1), x3)
  lst <- ls(env)
  sum(unlist(mget(lst[lst >= x1 & lst <= x3], envir = env)))
}
eval(parse(text = gsubfn(pat, Sum, s)))
## [1] TRUE

Upvotes: 1

Related Questions