0

I'm trying to resolve the loop so that it iterates over the year_value and resolves to 2017, 2018, and 2019 but keep getting an error message

Error in eval(lhs, parent, parent) : object 'Import_API_' not found

The data here is actually not important. I already have three existing objects: Import_API_2017, Import_API-2018, and Import_API_2019. What I want is to do some data cleaning and get MyData_2017 by reading and doing the cleaning in Import_API_2017 and so on and so forth. I don't want to essentially rewrite the whole thing all over again for 2018 and 2019.

 year_value <- c(2017:2019)

for (i in year_value){

MyData_[i] <- Import_API_[i]%>%
 row_to_names(row_number = 1) %>% 
 mutate(across(where(is.character), str_trim)) %>% 
 rename(Commodity_Code = I_COMMODITY, Commodity_Description = I_COMMODITY_SDESC) %>% 
 filter(SUMMARY_LVL == "DET")


 }
user2916331
  • 159
  • 4
  • 14
  • 1
    Put your `import_api_*` objects in a list: `import_api = list(Import_API_2017, Import_API_2018, Import_API_2019)`, then you can use `import_api[[i]]` to refer to them (with `i` being 1, 2, or 3), or take the extra step to name the list `names(import_api) = paste("year", 2017:2019, sep = "_")` and you can access them with, e.g., `import_api[["year_2018"]]` – Gregor Thomas Dec 03 '20 at 14:50
  • For further discussion, see my answer at [How to make a list of data frames](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames/24376207#24376207). – Gregor Thomas Dec 03 '20 at 14:51
  • `Import_API_[i]` with `i = 2017` is not the same as `Import_API_2017`. As noted you can move your dataframes to a list (`mylist`), wrap your df mods in a function and then process via `lapply`: `updates – SteveM Dec 03 '20 at 14:58

1 Answers1

0

It looks like the data you are attempting to read from cannot be found, so all manipulations on "Import_API_" will then fail, as "Import_API_" does not exist.

Are you sure all the objects exists in the working directory?

You can use getwd() and setwd() to respectively see and change the working directory.

aryashah2k
  • 120
  • 1
  • 9