【问题标题】:Object isn't found during repeat-loop but still appears in the global environment in R在重复循环期间找不到对象,但仍出现在 R 的全局环境中
【发布时间】:2018-03-06 23:49:13
【问题描述】:

我试图在重复循环完成后删除一个对象,但我收到警告消息“在 rm("games") 中:找不到对象 'games'”。但是,它仍然在全球环境中(这就是为什么我想首先删除它)。为什么会发生这种情况?如何在循环结束时删除“游戏”?

games <- data.frame(wins=c(0,2),seconds=c(2,0))

foo <- function(n) {

  count <<- 0  
  repeat {    
    if (count == n) {
      rm("games")
      break
    }    
  count <<- count + 1
  cat("New count is",count,"\n")    
 }
}

【问题讨论】:

    标签: r repeat


    【解决方案1】:

    阅读rm 的帮助,它只会从指定的框架(在本例中只是函数环境)中删除,除非你告诉它使用inherits=TRUE 遍历所有父框架。所以使用:

    rm("游戏",inherits=TRUE)

    请注意,对于删除其环境之外的某些内容的函数来说,这可能不是很好的样式,但您可能有一个很好的理由...

    【讨论】:

      猜你喜欢
      • 2018-12-23
      • 2018-10-27
      • 2022-11-22
      • 2019-12-16
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多