Vaibhav
Vaibhav

Reputation: 21

Get column names for all dataframes in R

I have large number of dataframes in R. Now I want to have a readable output for all column names against each dataframe

Let us say there are three dataframes A, B, C with with different number of columns and different column names as c("Col1", "Col2","Col3"), c("Col4", "Col5") and c("Col6", "Col7", "Col8", "Col9", "COl10") respectively

Now I want to have a output like this

enter image description here

Note: My intention is to later write it in a .csv file and break column names as per requirement (separated by tabs or "," separated)

Upvotes: 1

Views: 758

Answers (3)

Sathish
Sathish

Reputation: 12703

I am putting my work here, although it is similar to r2evans's solution.

Data

A <- data.frame(col1=1:2, col2=1:2, col3=1:2)
B <- data.frame(col4=1:2, col5=1:2)
C <- data.frame(col6=1:2, col7=1:2, col8=1:2, col9=1:2, col10=1:2)

Code

DataFrameName = c('A', 'B', 'C')
data.frame(DataFrameName    = DataFrameName, 
           Columns          = sapply(DataFrameName, function(x) paste(names(get(x)), collapse = ",")), 
           stringsAsFactors = FALSE)

Output

#   DataFrameName                   Columns
# A             A            col1,col2,col3
# B             B                 col4,col5
# C             C col6,col7,col8,col9,col10

Upvotes: 2

akrun
akrun

Reputation: 886938

With tidyverse, we can get the datasets in a list with lst, then loop over the list, get the column names, convert it to string, get the list of named strings into a two column tibble with enframe and unnest the 'Columns'

library(dplyr)
library(tidyr)
librarry(purrr)
lst(A, B, C) %>%
    map(~ .x %>% names %>% toString) %>%
    enframe(name = "DataFrameName", value = "Columns") %>%
    unnest(c(Columns))
# A tibble: 3 x 2
#  DataFrameName Columns                      
#  <chr>         <chr>                        
#1 A             Col1, Col2, Col3             
#2 B             Col1, Col5                   
#3 C             Col6, Col7, Col8, Col9, Col10

data

A <- data.frame(Col1 = 1:5, Col2 = 6:10, Col3 = 11:15)
B <- data.frame(Col1 = 1:5, Col5 = 6:10)
C <- data.frame(Col6 =1:5, Col7 = 6:10, Col8 = 6:10, Col9 = 7:11, Col10 = 11:15)

Upvotes: 1

r2evans
r2evans

Reputation: 160407

Here's a stab.

df1 <- data.frame(a=1,b=2,c=3)
df2 <- data.frame(A=1,E=2)
df3 <- data.frame(quux=7,cronk=9)
dfnms <- rownames(subset(ls.objects(), Type %in% c("data.frame", "tbl_df", "data.table")))
dfnms
# [1] "df1" "df2" "df3"
data.frame(name = dfnms, columns = sapply(mget(dfnms), function(x) paste(colnames(x), collapse = ",")))
#     name    columns
# df1  df1      a,b,c
# df2  df2        A,E
# df3  df3 quux,cronk

If you really need them double-quoted, then add dQuote, as in

data.frame(name = dfnms, columns = sapply(mget(dfnms), function(x) paste(dQuote(colnames(x)), collapse = ",")))
#     name        columns
# df1  df1    "a","b","c"
# df2  df2        "A","E"
# df3  df3 "quux","cronk"

Upvotes: 4

Related Questions