'arg'必须为NULL或字符向量

时间:2014-03-03 11:05:53

标签: r function arguments

这是prop.test函数:

baby.prop.test = function (x, n, p, conf.level = 0.95) {
  # ...
  return(prop.test(x,n,p,conf.level))
  #baby.prop.test$statistic
}
# test case
baby.prop = baby.prop.test(72, 100, .7, conf.level=.99)
stopifnot(isTRUE(all.equal(as.numeric(baby.prop$statistic), .43643578)))
stopifnot(isTRUE(all.equal(as.numeric(baby.prop$p.value), .66252058)))

这是错误:

Error in match.arg(alternative) : 
  'arg' must be NULL or a character vector

知道什么是错的吗?

1 个答案:

答案 0 :(得分:6)

根据formals(prop.test)?prop.test,第四个参数称为alternative,必须是c("two.sided", "less", "greater")的字符。你的第四个元素是conf.level(这是prop.test的第五个元素,顺序很重要)。要“忽略”参数的顺序,你必须为你的参数命名(至少是conf.level):

prop.test(x, n, p, conf.level=conf.level)