ks ramana
ks ramana

Reputation: 133

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

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?

Upvotes: 2

Views: 61

Answers (1)

akrun
akrun

Reputation: 886938

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))
    }

Upvotes: 1

Related Questions