Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to extract column names of dataframes from within a list in R

I would like to extract (and change) the column names of dataframes within a list by indexing them (to use in a for loop).

For example this gives me the desired result

df_list <- list(df = data.frame(x = c(1,2,3), y = c(4,5,6))
colnames(df_list$df)
[1] "x" "y"

However the following does not:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df_list <- list(df = data.frame(x = c(1,2,3), y = c(4,5,6))
colnames(df_list[1])
NULL

How can I do this by referring to the index of the dataframe rather than its name

Edit: Solved by thelatemail’s comment

>Solution :

Just use double braket [[..]] to access single element in list in R.

The [..] method returns objects of class list while the [[..]] method returns objects whose class is determined by the type of their values (i.e. a dataframe in our case).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading