Reputation: 143
I encounter a problem with a loop that creates multiple data frames with different names and assign them a value.
I have a big data frame with different car manufacturers and their CO2 emissions (at the car model level).
After dividing my big data frame into data frames for each manufacturer, I'm trying to subset each of them with their 75% quartile (best 75% cars that pollute the less).
The wrong way (but works) :
subFord <- subset(ford, ford$co2_emissions <= quantile(ford$co2_emissions, 0.75))
subDaimler <- subset(daimler, daimler$co2_emissions <= quantile(daimler$co2_emissions, 0.75))
subGM <- subset(gm, gm$co2_emissions <= quantile(gm$co2_emissions, 0.75))
What I'm trying to do (doesn't work):
manufacturer <- c('ford', 'daimler', 'gm')
for(i in manufacturer) {
paste('sub', i, sep = '') <- subset(i, i$co2_emissions <= quantile(i$co2_emissions, 0.75))
}
Any help is welcomed.
Upvotes: 0
Views: 2042
Reputation: 741
you'll want to use assign.
assign(paste('sub', i, sep = ''), subset(i, i$co2_emissions <= quantile(i$co2_emissions, 0.75)))
Upvotes: 1