I am not sure if this will work exactly for your case since I don't have sample data, but if you were to do what I'm thinking you are looking for with the mtcars data set, it would be something like this...First, it might be best to have a list of data frames to house the data you are running the model on. This can be done as follows:
library(dplyr)
library(randomForest)
dfs <- list() #home for the list of dataframes on which to run a randomforest
set.seed(1)
for(i in 1:5){
dfs[[i]] <- sample_n(mtcars, size = 10, replace = FALSE)
}
(Per the comments, a slicker way to do this would be to go with
dfs_slicker_approach <- lapply(seq(5),
function(i) sample_n(mtcars, size = 10, replace = FALSE))
)
The dfs list now contains a list of data.frames which contain 10 randomly selected rows from the mtcars data set. (Obviously, you'll want to update this to fit your needs.)
Then we run the randomForest function on this list using the lapply function as follows:
rfs <- lapply(dfs, function(m) randomForest(mpg ~ .,
data = m, importance = TRUE ))
Again, change the syntax to select the columns you are interested in predicting on. The rfs list now contains all of our randomForest objects. You can again access these using lapply. For instnace, if we want the predicted values, we can do this as follows: (We'll subset to only the first set of predictions to avoid printing a a lot of info)
> lapply(rfs, as.data.frame(predict))[1]
[[1]]
value
Merc 230 22.85464
Merc 450SE 17.61810
Fiat 128 22.31571
Porsche 914-2 23.95909
Valiant 21.28786
Pontiac Firebird 15.93824
Ford Pantera L 21.20373
Chrysler Imperial 14.40740
Lincoln Continental 16.43074
Mazda RX4 Wag 21.18467