1

Below is my code so far, which doesn't work. I have 7 dataframes that each contain information about one pollutant (named in pollutant_long_name). Each of these dataframes is called, for example, national_tier1_Nitrogen Oxides or national_tier1_Ammonia. I've been trying to use the paste0 function to use the values in my for loop sequence within the for loop, but it doesn't seem to be working. The first error I got is

Error: unexpected '=' in: " dplyr::group_by( inv_sector ) %>%
dplyr::summarise( paste0( "NEI_emissions_", pollutant_name, "_2011" ) ="

Basically, my end goal is the have 7 dataframes called total_2011_pollutant name that I'll then leftjoin to make a dataframe containing all 7 pollutants.

pollutant_long_name <- c( "Nitrogen Oxides", "Sulfur Dioxide", "Carbon Monoxide", "Volatile Organic Compounds",
                          "PM2.5 Filterable", "PM10 Filterable", "Ammonia" )
for( n in pollutant_long_name ){
  pollutant_name <- paste0( n )
#===================================================================================NOX
NEI_2011 %>%
  dplyr::left_join( CEDS_to_EPA_rename, by = c( "CEDS_Sector" ) ) %>%
  dplyr::filter( pollutant == pollutant_name ) %>%
  dplyr::group_by( inv_sector ) %>%
  dplyr::summarise( paste0( "NEI_emissions_", pollutant_name, "_2011" ) = sum( emissions/1000 ) ) %>%
  round_df( digits = 0 )-> NEI_2011_emissions

paste0( "national_tier1_", pollutant_name ) %>%
  dplyr::select( c(Source.Category, X2011) ) %>%
  dplyr::rename( inv_sector = Source.Category ) %>%
  dplyr::mutate( X2011 = gsub( ",", "", X2011 ) ) %>%
  dplyr::mutate( X2011 = as.numeric(as.character( X2011 ) ) ) %>%
  dplyr::rename( national_2011_NOX = X2011 )-> paste0( "national_2011_", pollutant_name )

NEI_2011_emissions %>%
  dplyr::left_join( paste0( "national_2011_", pollutant_name ), by = c( "inv_sector" ) ) -> paste0( "total_2011_", pollutant_name )
paste0( "total_2011_", pollutant_name )[is.na(paste0( "total_2011_", pollutant_name ))] <- 0
}

1 Answer 1

1

If we use string on the lhs of =, it wouldn't work, instead we can make use := while evaluating the string with (!!)

library(dplyr)
NEI_2011 %>%
  dplyr::left_join( CEDS_to_EPA_rename, by = c( "CEDS_Sector" ) ) %>%
  dplyr::filter( pollutant == pollutant_name ) %>%
  dplyr::group_by( inv_sector ) %>%
  dplyr::summarise( !!paste0( "NEI_emissions_",  ## changed
       pollutant_name, "_2011" ) := sum( emissions/1000 ) ) %>% ## changed
  round_df( digits = 0 )-> NEI_2011_emissions

For the second chunk to create the object name with paste, it is still a string. If we need to get the value of the string object then use get (for multiple objects use mget - returns a list of object values). To assign the output to an object, use assign

assign(paste0( "national_2011_", pollutant_name ), 
   get(paste0( "national_tier1_", pollutant_name )) %>%
   dplyr::select( c(Source.Category, X2011) ) %>%
   dplyr::rename( inv_sector = Source.Category ) %>%
   dplyr::mutate( X2011 = gsub( ",", "", X2011 ) ) %>%
   dplyr::mutate( X2011 = as.numeric(as.character( X2011 ) ) ) %>%
   dplyr::rename( national_2011_NOX = X2011 ))

Generally, it is not really needed to assign multiple objects in the global environment. We could create a single list to store the output of different values

Sign up to request clarification or add additional context in comments.

2 Comments

thank you! I'm now getting Error in UseMethod("select_") : no applicable method for 'select_' applied to an object of class "character" for the second chunk there, all columns are factors, which I didn't think would matter for select and rename to work?
@MarideeWeber. If you meant paste0( "national_tier1_", pollutant_name ) %>% it is a string object, you need get(paste0( "national_tier1_", pollutant_name )) to get the values of that object

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.