0

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.

1 Answer 1

1

you'll want to use assign.

assign(paste('sub', i, sep = ''), subset(i, i$co2_emissions <= quantile(i$co2_emissions, 0.75)))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but I get an error "i$co2_emission is invalid for atomic vectors ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.