0

Here is a snipit of my code:

parameters$x<-0
parameters$y<-0
for (x in c(1:2)){
  for (y in c(10,20,30,40,50,60,70,80,100)){
control <- rpart.control(minsplit = x,
                         minbucket = round(y),
                         maxdepth = 10,
                         cp = 0)
tune_fit <- rpart(survived~., data = data_train, method = 'class', control = control)

parameters[x] <-accuracy_tune(tune_fit)
}}

I basically want a table that has the x rows and y columns (based on what my x and y inputs are in the for loop) and then storing the output of the function as a value in this new table. I recognize what I am trying to do with parameters is not working.

coder
  • 8,346
  • 16
  • 39
  • 53
mayakhar
  • 1
  • 1
  • 4
    Welcome to Stack Overflow! Please provide [example data](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) in order to make your issue reproducible! – jay.sf Jul 24 '18 at 17:24
  • *is not working* ... is not helpful for us. Please post errors or undesired results. – Parfait Jul 24 '18 at 18:33

1 Answers1

0

With this solution, whatever result accuracy_tune returns will get stored in the matrix. Although, you haven't provided much information, but here's my solution:

# create matrix to store output
parameters <- matrix(data=NA, nrows=2, ncols=10)

# value to use for iteration
x_vals <- c(1:2))
y_vals <- c(10,20,30,40,50,60,70,80,100))

# iterate over values
for (x in seq(x_vals){
  for (y in seq(y_vals){
control <- rpart.control(minsplit = x,
                         minbucket = round(y),
                         maxdepth = 10,
                         cp = 0)
tune_fit <- rpart(survived~., data = data_train, method = 'class', control = control)

parameters[x,y] <-accuracy_tune(tune_fit)
}}

# set row and column names
colnames(parameters) <- y_vals
row.names(parameters) <- x_vals
YOLO
  • 20,181
  • 5
  • 20
  • 40