什么是“错误:对象'< myvariable>"找不到"意思是? [英] What does "Error: object '<myvariable>' not found" mean?

查看:297
本文介绍了什么是“错误:对象'< myvariable>"找不到"意思是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误消息:

错误:找不到对象"x"

Error: object 'x' not found

或更复杂的版本

均值(x)中的错误: 选择函数'mean'的方法时评估参数'x'时出错:错误:未找到对象'x'

Error in mean(x) : error in evaluating the argument 'x' in selecting a method for function 'mean': Error: object 'x' not found

这是什么意思?

推荐答案

该错误表示R找不到错误消息中提到的变量.

The error means that R could not find the variable mentioned in the error message.

重现错误的最简单方法是键入不存在的变量的名称. (如果已经定义了x,请使用其他变量名称.)

The easiest way to reproduce the error is to type the name of a variable that doesn't exist. (If you've defined x already, use a different variable name.)

x
## Error: object 'x' not found

错误的更复杂版本具有相同的原因:x不存在时调用一个函数.

The more complex version of the error has the same cause: calling a function when x does not exist.

mean(x)
## Error in mean(x) : 
##   error in evaluating the argument 'x' in selecting a method for function 'mean': Error: object 'x' not found

一旦定义了变量,就不会发生错误.

Once the variable has been defined, the error will not occur.

x <- 1:5
x
## [1] 1 2 3 4 5     
mean(x)
## [1] 3


您可以使用 ls 来检查变量是否存在 exists .


You can check to see if a variable exists using ls or exists.

ls()        # lists all the variables that have been defined
exists("x") # returns TRUE or FALSE, depending upon whether x has been defined.


当您使用非标准评估时,可能会发生此类错误.例如,当使用 subset 时,如果列名出现错误在子集中的数据框中不存在.


Errors like this can occur when you are using non-standard evaluation. For example, when using subset, the error will occur if a column name is not present in the data frame to subset.

d <- data.frame(a = rnorm(5))
subset(d, b > 0)
## Error in eval(expr, envir, enclos) : object 'b' not found


如果使用自定义评估,也会发生该错误.


The error can also occur if you use custom evaluation.

get("var", "package:stats") #returns the var function
get("var", "package:utils")
## Error in get("var", "package:utils") : object 'var' not found

在第二种情况下,在以下情况下找不到 var 函数R在utils软件包的环境中查找,因为utils search 列出stats.

In the second case, the var function cannot be found when R looks in the utils package's environment because utils is further down the search list than stats.

在更高级的用例中,您可能希望阅读:

In more advanced use cases, you may wish to read:

  • The Scope section of the CRAN manual Intro to R and demo(scoping)
  • The Non-standard evaluation chapter of Advanced R

这篇关于什么是“错误:对象'&lt; myvariable&gt;"找不到"意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆