Hello,
I'm trying to create a series of randomForest objects, basically in a
loop like this:
forests <- list();
for (level in 1:10) {
# do some other things here
# create a random forest
forest <- randomForest(
x = x.level,
y = z.level,
ntree = trees
);
forests <- c(forests, forest);
}
But instead of creating a list of 10 forests, this creates a list of
180 elements, 18 for each forest. Is there a way to create a list of
randomForest objects for use later on in code like:
for (forest in forests) {
values <- predict(forest, data);
# do things with these predicted values
}
Sincerely,
Paul
change forests <- c(forests, forest); into forests[[level]] <- forest On 1/6/07, Paul Boutros <paul.boutros at utoronto.ca> wrote:> Hello, > > I'm trying to create a series of randomForest objects, basically in a > loop like this: > > forests <- list(); > > for (level in 1:10) { > > # do some other things here > > # create a random forest > forest <- randomForest( > x = x.level, > y = z.level, > ntree = trees > ); > > forests <- c(forests, forest); > > } > > > But instead of creating a list of 10 forests, this creates a list of > 180 elements, 18 for each forest. Is there a way to create a list of > randomForest objects for use later on in code like: > > for (forest in forests) { > values <- predict(forest, data); > # do things with these predicted values > } > > Sincerely, > Paul > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Weiwei Shi, Ph.D Research Scientist GeneGO, Inc. "Did you always know?" "No, I did not. But I believed..." ---Matrix III
Howabout using 'lapply'?
forest <- lapply(1:10, function(level) {
# do some other things here
# create and return a random forest
randomForest(x = x.level, y = z.level, ntree = trees)
})
# give meaningful names
names(forest) <- paste("level", 1:10, sep = "_")
Paul Boutros wrote:> Hello,
>
> I'm trying to create a series of randomForest objects, basically in a
> loop like this:
>
> forests <- list();
>
> for (level in 1:10) {
>
> # do some other things here
>
> # create a random forest
> forest <- randomForest(
> x = x.level,
> y = z.level,
> ntree = trees
> );
>
> forests <- c(forests, forest);
>
> }
>
>
> But instead of creating a list of 10 forests, this creates a list of
> 180 elements, 18 for each forest. Is there a way to create a list of
> randomForest objects for use later on in code like:
>
> for (forest in forests) {
> values <- predict(forest, data);
> # do things with these predicted values
> }
>
> Sincerely,
> Paul
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Eric Archer, Ph.D.
NOAA-SWFSC
8604 La Jolla Shores Dr.
La Jolla, CA 92037
858-546-7121,7003(FAX)
eric.archer at noaa.gov
"Lighthouses are more helpful than churches."
- Benjamin Franklin
"...but I'll take a GPS over either one."
- Craig George