0
randForest=function(s,d)
{
  t=d$s
  mydata=d
  t=as.factor(t)
  index=sample(1:nrow(mydata),size=nrow(mydata)*0.8,replace=FALSE)
  training=mydata[index,]
  a=data.frame(training)
  testing=mydata[-index,]
  b=data.frame(testing)
  tv=training$t
  rf=randomForest(t~.,data=a,mtry=4,ntree=2001,importance=TRUE)
  print(rf)
}

library(randomForest)

So I am creating a function which accepts a column name of the passed dataset as an argument and then I split the data into train and test set and to use it with train and test dataset(like train$s) but somehow I dont know how to do that

calling the function as

randForest(Body mass index,mydata)

it gives Body mass index object not found

the only way it works is calling it like

randForest(mydata$'Body mass index',mydata)

but again I cant use that first argument to access that same column of the train or test dataset even if I store it like tv=mydata$'Body mass index' and then type training$tv it doesnt seem to work

A suggestion would be really appreciated on how to handle names of the column and solve my issue

0 Answers0