Home > Software design >  Column names of data frame into list
Column names of data frame into list

Time:06-29

I have a data frame with over 700 columns. I wondered if there was any way of creating a new table with a list of these column names and their corresponding column number that I can refer back to?

CodePudding user response:

Something like this?

 data.frame(col_no = 1:ncol(iris), variable = names(iris))
  col_no     variable
1      1 Sepal.Length
2      2  Sepal.Width
3      3 Petal.Length
4      4  Petal.Width
5      5      Species

CodePudding user response:

you can also store the names in a named list:

col_names <- names(df)

names(col_names) <- 1:length(col_names)


output:
 1   2   3 
"x" "y" "c" 
  •  Tags:  
  • r
  • Related