【问题标题】:R - Object not found in user-defined functionR - 在用户定义的函数中找不到对象
【发布时间】:2016-08-04 00:43:34
【问题描述】:

所以我之前曾多次看到这个问题,但我尝试过的其他答案似乎都不起作用。当我运行以下函数时,我得到:“错误:找不到对象'x'。”

xpermntotable <- function(x,y,z){
  xmovematrix <- matrix(unlist(xpermn), ncol = 3, byrow = TRUE)
  for(i in 1:120){
    xmove1 <- xmovematrix[i,1]
    xmove2 <- xmovematrix[i,2]
    xmove3 <- xmovematrix[i,3]
    print(x)
    xtemp <- filter_(fullTable, .dots=list(
      bquote(.(as.name(xmove1)) == x), 
      bquote(.(as.name(xmove2)) == y), 
      bquote(.(as.name(xmove3)) == z)))
    xwin <- rbind(xwin, xtemp)
  }
}

xpermntotable(1,2,3)

问题似乎根源于下面函数的特定部分,其中“x”(可能是“y”和“z”)没有被正确读取:

xtemp <- filter_(fullTable, .dots=list(
          bquote(.(as.name(xmove1)) == x), 
          bquote(.(as.name(xmove2)) == y), 
          bquote(.(as.name(xmove3)) == z)))

我尝试过的解决方案:

  • 在 as.name(x) 或 eval(x) 中包含“x”变量

【问题讨论】:

    标签: r function user-defined-functions


    【解决方案1】:

    所以在深入研究 bquote 函数之后,我意识到我的解决方案一直存在。 x、y 和 z 变量需要用

    包装
    .() 
    

    以下代码有效:

    xpermntotable <- function(x,y,z){
      xmovematrix <- matrix(unlist(xpermn), ncol = 3, byrow = TRUE)
      for(i in 1:120){
        xmove1 <- xmovematrix[i,1]
        xmove2 <- xmovematrix[i,2]
        xmove3 <- xmovematrix[i,3]
        xtemp <- filter_(fullTable, .dots=list(
          bquote(.(as.name(xmove1)) == .(x)), 
          bquote(.(as.name(xmove2)) == .(y)), 
          bquote(.(as.name(xmove3)) == .(z))))
        xwin <- rbind(xwin, xtemp)
        return(xwin)
      }
    }
    
    
    xwin <- xpermntotable(1,2,3)
    

    更多关于 bquote 函数here的信息。

    【讨论】:

    • 对。你没有看说明书。我认为这只不过是 yopu 现在纠正的错字而已。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    相关资源
    最近更新 更多