5

我正在尝试编写一个生成字符串向量的函数,每个字符串都在全局环境中作为表达式进行评估。问题是 eval(parse(text=x)) 仅在函数的环境内进行评估。

作为一个假设的例子,假设我想用 NA 替换几个变量的值,但前提是它们低于某个截止值。

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

而不是一遍又一遍地复制和粘贴同一行,而是更改每一行中的相同文本......

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...

...我试图让 R 为我做这件事:

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"

该解决方案有效,但我想将最后一个 for 循环放在函数内部。有任何想法吗?

编辑:谢谢,凯文。这是“功能”版本(哇哈哈,我有时会忍不住):

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")
4

2 回答 2

6

必须有更好的解决方案。例如,对于您的示例,这有效:

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

这都给了:

> 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
于 2013-09-19T04:35:37.390 回答
5

eval有一个参数 ,envir它允许您指定要在其中评估表达式的环境。所以,

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

应该有希望工作。

于 2013-09-19T04:11:57.123 回答