在函数 [r] 中设置默认值时遇到问题

trouble setting default values in a function[r]

信息:

目标:

代码问题:

cor_v1_v2 = function(v1, v2 = state.x77[,"Life Exp"], method = "pearson"){
  cor(v1,v2,method = "pearson")
}

我尝试了几种变体,例如:

cor_v1_v2 = function(v1, v2 = state.x77[,"Life Exp"], method = "input"){
  cor(v1,v2,method = "input")
}

和:

cor_v1_v2 = function(v1, v2 = state.x77[,"Life Exp"], method = "pearson"){
  cor(v1,v2,method)
}

我得到 return 每个使用错误。我知道这只是我的语法和想法,但我仍然卡住了。

我想你只是想要

cor_v1_v2 = function(v1, v2 = state.x77[,"Life Exp"], method = "pearson"){
  cor(v1, v2, method = method)
}

这将默认为 "pearson",但可以更改为您想要的任何内容。