R:编写一个让我们在数据集之间切换的函数

时间:2016-12-06 11:55:06

标签: r function

我正在尝试编写一个函数,让我选择用于数据分析的数据部分(预测变量)(lasso& knn)。

基本上我正在寻找的是使用p()选择3个预测变量或14个来进行分析。

p <- function(x) { # Choose p(3) or p(14) to use X predictors in analysis.
  if ( x == 3 ) {
  df <- FullData[1:4]
  train = df[ 1:750 , ] # training set
  test = df[ 751:1500 , ] # test set
  train.x = cbind( train[2:4] )
  test.x = cbind( test[2:4] )
} 
  else if ( x == 14 ) {
  train = FullData[ 1:750 , ] # training set
  test = FullData[ 751:1500 , ] # test set
  train.x = cbind( train[2:15] )
  test.x = cbind( test[2:15] )
}}

我尝试了'if else'的不同组合if if,if'或'if,else',但我似乎无法解决这个问题。我上面的代码有很多变化,这个似乎产生了最少的错误。

我得到这个错误:

Error: unexpected 'if' in: " test.x = cbind( test[2:4] ) } if"

或者这个:

Error: unexpected '}' in "}"

0 个答案:

没有答案