简体   繁体   English

尝试运行以下代码中的插补函数时,“match.arg(what) 中的错误:'arg' 必须为 NULL 或字符向量”

[英]"Error in match.arg(what) : 'arg' must be NULL or a character vector" when trying to run the impute function in the below code

I'm trying to impute the missing values with mean in a column in the dataset.我试图在数据集中的一列中用均值来impute缺失值。 Using impute function available in Hmisc package使用Hmisc包中提供的Hmisc功能

Have tried running several set of codes.试过运行几组代码。 In past I've used the piece of code & on same dataset, however, now it's not running.过去我在同一个数据集上使用了这段代码,但是,现在它没有运行。

impute(crime$average.ed,mean)
crime$average.ed<-as.numeric(impute(crime$average.ed, mean))
summary(crime)

The missing values in the variable average.ed must replaced with mean.变量average.ed的缺失值必须替换为均值。 I keep getting error我不断收到错误

Error in match.arg(what) : 'arg' must be NULL or a character vector match.arg(what) 中的错误:'arg' 必须为 NULL 或字符向量

(BTW mean is 10.51) (顺便说一句,平均值是 10.51)

Finally found the solution to the problem by myself.终于自己找到了解决问题的方法。 A package named e1071 was causing a problem when using with package Hmisc.与包 Hmisc 一起使用时,名为 e1071 的包会导致问题。 Both the packages have impute function and when used provided when both the packages are active, it won't run.这两个包都具有估算功能,并且在两个包都处于活动状态时提供时,它不会运行。 So moral of the story use any one of the package.所以故事的寓意使用了任何一个包。

The answer provided by @Rohan is 100% spot on. @Rohan 提供的答案是 100% 准确的。 Yet, I want to make a few additions for people stuck with the same problem.然而,我想为遇到同样问题的人做一些补充。

TL;DR:特尔;博士:
Replace in your code the references to impute with Hmisc::impute(matrix_to_operate_on, replacement_value)在您的代码中将要Hmisc::impute(matrix_to_operate_on, replacement_value)的引用替换为Hmisc::impute(matrix_to_operate_on, replacement_value)

The impute arguments list can be found in this doc, https://www.rdocumentation.org/packages/Hmisc/versions/4.4-1/topics/impute . impute参数列表可以在这个文档中找到, https://www.rdocumentation.org/packages/Hmisc/versions/4.4-1/topics/impute

For geeks:对于极客:
The fraudulent/unwanted impute function is the one defined in the library e1071 , https://www.rdocumentation.org/packages/e1071/versions/1.7-4/topics/impute .欺诈/不需要的impute函数是在库e1071定义的函数, https: e1071

How to diagnose the problem?如何诊断问题?
In R there is a introspection functionality called body() , https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/body .在 R 中有一个名为body()的内省功能, https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/body This will allow you to view the source of the actual function that you are invoking.这将允许您查看正在调用的实际函数的来源。

In the problematic case this would look like:在有问题的情况下,这看起来像:

> body(impute)
{
    what <- match.arg(what)
    if (what == "median") {
        retval <- apply(x, 2, function(z) {
            z[is.na(z)] <- median(z, na.rm = TRUE)
            z
        })
    }
    else if (what == "mean") {
        retval <- apply(x, 2, function(z) {
            z[is.na(z)] <- mean(z, na.rm = TRUE)
            z
        })
    }
    retval
}

After investigating the body of the impute function, you are able to make a guess where is the error above coming from, Error in match.arg(what) : 'arg' must be NULL or a character vector在调查了 impute 函数的主体后,您可以猜测上面的错误来自哪里Error in match.arg(what) : 'arg' must be NULL or a character vector

Now you know the problem, so just reference the right function by prepending the correct library Hmisc:: to the impute call.现在您知道问题所在了,因此只需通过将正确的库Hmisc::到估算调用来引用正确的函数即可。

Good practises:良好做法:
To avoid such sticky situation in future, always prepend the library where the function is coming from.为了避免将来出现这种棘手的情况,请始终在函数来自的库之前加上。 You never know if there aren't any other R functions which have "evil" twins.您永远不知道是否有其他 R 函数具有“邪恶”双胞胎。 From [package] import [function] in R 在 R 中从 [包] 导入 [函数]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:match.arg中的错误:&#39;arg&#39;必须为NULL或字符向量 - Warning: Error in match.arg: 'arg' must be NULL or a character vector match.arg(regions) 中的错误:“arg”必须为 NULL 或字符向量 - Error in match.arg(regions) : 'arg' must be NULL or a character vector 运行应用程序时出现闪亮错误:match.arg(position) 中的错误:&#39;arg&#39; 必须为 NULL 或字符向量 - Shiny Error when running App: Error in match.arg(position) : 'arg' must be NULL or a character vector match.arg(opt_crit)中的错误:“ arg”必须为NULL或字符向量 - Error in match.arg(opt_crit) : 'arg' must be NULL or a character vector Shiny 应用程序中的“match.arg(position) 错误:‘arg’必须为 NULL 或字符向量” - "Error in match.arg(position) : 'arg' must be NULL or a character vector" in Shiny app match.arg(p.adjust.method)中的错误:“ arg”必须为NULL或字符向量 - Error in match.arg(p.adjust.method) : 'arg' must be NULL or a character vector match.arg(position) 中的闪亮错误:“arg”必须为 NULL 或字符向量 - shiny Error in match.arg(position) : 'arg' must be NULL or a character vector match.arg(mvnTest)中的错误:“ arg”的长度必须为1 - Error in match.arg(mvnTest) : 'arg' must be of length 1 &#39;arg&#39;必须为NULL或字符向量 - 'arg' must be NULL or a character vector list.arg函数用于list参数 - match.arg function for list argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM