Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

eval(parse(text=x)) inside a function, how to evaluate in global environment?

Tags:

r

I'm trying to write a function that generates a vector of strings, each of which are evaluated as expressions in the global environment. The problem is that eval(parse(text=x)) only evaluates inside the function's environment.

As a hypothetical example, say that I want to replace several variables' values with NA, but only if they're below a certain cutoff value.

set.seed(200)
df <- as.data.frame(matrix(runif(25), nrow=5, ncol=5))
df
         V1         V2        V3        V4         V5
1 0.5337724 0.83929374 0.4543649 0.3072981 0.46036069
2 0.5837650 0.71160009 0.6492529 0.5667674 0.09874701
3 0.5895783 0.09650122 0.1537271 0.1317879 0.20659381
4 0.6910399 0.52382473 0.6492887 0.9221776 0.92233983
5 0.6673315 0.23535054 0.3832137 0.6463296 0.31942681

cutoff.V1 <- 0.9
cutoff.V2 <- 0.5
cutoff.V3 <- 0.1
cutoff.V4 <- 0.7
cutoff.V5 <- 0.4

Rather than copy-and-pasting the same line over and over, changing the same text in each line...

df$V1[df$V1 < cutoff.V1] <- NA
df$V2[df$V2 < cutoff.V2] <- NA
df$V3[df$V3 < cutoff.V3] <- NA
df$V4[df$V4 < cutoff.V4] <- NA
df$V5[df$V5 < cutoff.V5] <- NA
# ad infinitum...

...I'm trying to have R do it for me:

vars <- c("V1", "V2", "V3", "V4", "V5")

variable.queue <- function(vec, placeholder, command) {
  x <- vector()
  for(i in 1:length(vec)) { 
    x[i] <- gsub(placeholder, vec[i], command) 
  }
  return(x)
}

commands <- variable.queue(vars, "foo", "df$foo[df$foo < cutoff.foo] <- NA")
for(i in 1:length(commands)) {eval(parse(text=commands[i]))}

df

  V1        V2        V3        V4        V5
1 NA 0.8392937 0.4543649        NA 0.4603607
2 NA 0.7116001 0.6492529        NA        NA
3 NA        NA 0.1537271        NA        NA
4 NA 0.5238247 0.6492887 0.9221776 0.9223398
5 NA        NA 0.3832137        NA        NA


# FYI the object "commands" is the vector of strings that I want evaluated

commands
[1] "df$V1[df$V1 < cutoff.V1] <- NA" "df$V2[df$V2 < cutoff.V2] <- NA" "df$V3[df$V3 < cutoff.V3] <- NA"
[4] "df$V4[df$V4 < cutoff.V4] <- NA" "df$V5[df$V5 < cutoff.V5] <- NA"

This solution works, but I want to put the last for-loop INSIDE the function. Any ideas?

Edit: Thanks, Kevin. Here's the "functional" version (bwahaha, I just can't help myself sometimes):

variable.queue <- function(vec, placeholder, command) {
  x <- vector()
  for(i in 1:length(vec)) { 
    x[i] <- gsub(placeholder, vec[i], command) 
  }
  for(i in 1:length(x)) {
    eval(parse(text=x[i]), envir= .GlobalEnv)
  }
}

variable.queue(vars, "foo", "df$foo[df$foo < cutoff.foo] <- NA")
like image 609
rsoren Avatar asked Sep 19 '13 04:09

rsoren


2 Answers

There has to be a better solution. E.g., for your example, this works:

set.seed(200)
df <- as.data.frame(matrix(runif(25), nrow=5, ncol=5))
cutoff <- c(0.9,0.5,0.1,0.7,0.4)

df[mapply("<", df,cutoff)] <- NA

#or
df[sweep(df,2,cutoff,"<")] <- NA

#or even
df[df < rep(cutoff,each=nrow(df))] <- NA

Which all give:

> df
  V1        V2        V3        V4        V5
1 NA 0.8392937 0.4543649        NA 0.4603607
2 NA 0.7116001 0.6492529        NA        NA
3 NA        NA 0.1537271        NA        NA
4 NA 0.5238247 0.6492887 0.9221776 0.9223398
5 NA        NA 0.3832137        NA        NA
like image 189
thelatemail Avatar answered Sep 28 '22 17:09

thelatemail


eval has an argument, envir, that allows you to specify the environment in which you want to evaluate your expression. So,

eval(parse(text=command[i]), envir=.GlobalEnv)

should hopefully work.

like image 41
Kevin Ushey Avatar answered Sep 28 '22 16:09

Kevin Ushey