Inaki Carril
Inaki Carril

Reputation: 61

Retrieve column names using a loop

I have a list with 14 data frames, i want to retrieve the column names of each data frame with a for loop.

files_list <- list(data_04, data_05, data_06, data_07, data_08, data_09, data_010, data_011,
                   data_012, data_013, data_015, data_016, data_017, data_018)

Basically i want to avoid doing this with each data frame.

colnames(data_04)

Upvotes: 1

Views: 129

Answers (2)

akrun
akrun

Reputation: 886938

We may also use sapply

sapply(files_list, colnames)

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388807

You can use lapply and extract column names from each dataframe.

lapply(files_list, colnames)

This can also be done with purrr::map similarly.

purrr::map(files_list, colnames)

As @Sotos suggested using names instead of colnames should also work for dataframes.

Upvotes: 1

Related Questions