将参数传递给函数错误

时间:2017-08-17 20:12:55

标签: r

当我用plink调用sapply时出现此错误

缺少

参数,没有默认值

任何让它发挥作用的想法?

plink = function(post, original_predictor1,original_predictor2){
  with(post,
      result = parameter_vector1*original_predictor1 + parameter_vector2*original_predictor2
  )
  return(result)
}

post = data.frame(parameter_vector1 = c(1,2),parameter_vector2 = c(9,5))
original_predictor1 = c(1,2) #original predictor variable x in y = beata*x
original_predictor2 = c(10,20) #original predictor variable x in y = beata*x
pred.raw = sapply(1:2, function(i) plink(original_predictor1[i],original_predictor2[i])) 

1 个答案:

答案 0 :(得分:2)

最大的问题是您没有将post作为第一个参数传递给plink函数。使用=with()函数内创建变量也不是一个好主意,因为它们被解释为命名参数。这应该工作

plink = function(post, original_predictor1, original_predictor2){
  result <- with(post, parameter_vector1*original_predictor1 + parameter_vector2*original_predictor2
  )
  return(result)
}

post = data.frame(parameter_vector1 = c(1,2),parameter_vector2 = c(9,5))
original_predictor1 = c(1,2) #original predictor variable x in y = beata*x
original_predictor2 = c(10,20) #original predictor variable x in y = beata*x
pred.raw = sapply(1:2, function(i) plink(post, original_predictor1[i],original_predictor2[i]))