Convert first row into column names for mulitiple dataframes in a loop

Viewed 50

I have multiple data frames. In each one of them, I need to convert the 1st row into column names

My code is as follows:

Assign all dataframes into cities

cities <- objects()
library(janitor)
for (i in cities){
  paste0("file_",i) <- assign(i, get(i) %>%  row_to_names(row_number = 1))
}

This code creates the following error:

Error in paste0("file_", i) <- assign(i, get(i) %>% row_to_names(row_number = 1)) : target of assignment expands to non-language object

How can I fix this problem?

1 Answers

We can use the paste0 inside thee assign

library(dplyr)
for (i in cities){
    assign(paste0("file_",i),  get(i) %>%  row_to_names(row_number = 1))
    }
Related