Home > OS >  Can't unlist and retain top level names in 1st column
Can't unlist and retain top level names in 1st column

Time:08-28

I am trying to unlist all variables from a list of length 3.

The data shows:

df_list   list3 (These are each sheet with row/column data)
sheet 1   tbl_df
sheet 2   tbl_df
sheet 3   tbl_df

I am trying to use:

as.data.frame(table(unlist(df_list,use.names = T,recursive = T)),row.names = T)

data.table(unlist(df_list),keep.rownames = T)

data.table(all_vars = unlist(df_list,recursive = T,use.names = T),keep.rownames = TRUE)

I all three scenarios I get returned one output column of all variables from the list elements that are listed

col1
all
all
all

Where I am trying to get

col1     col2
sheet 1  all
sheet 1  all
sheet 2  all
sheet 2  all
sheet 2  all
sheet 3  all
sheet 3  all

Is this possible? Thank you. I'd like to do some data checking with dplyr once this is done and run some group by's, remove na's from col2, and do some duplicate checks etc. But I really need those sheet names so I can see where the data is housed between sets. Thank you.

The dput is from using test <- data.table(all_vars = unlist(df_list),keep.rownames = T)

structure(list(all_vars = c("searchs,top 10,austintexas.gov,website", 
"dog,dogs,pet,off-leash,areas,area", "cat,dog,pet,stray,austin animal center,no kill,data,open government,map", 
"dangerous,dogs,public safety,pets,animals", NA, "restaurant,geodata,health,fdhealth", 
"fire,afd,stations,fire stations", "unclaimed,property,unclaimed property", 
"water quality,eii,ali,barton springs,lady bird lake,lake austin,salamanders,barton creek,bull creek,bacteria,coli", 
"google,fiber,gigabit,tara,community,connections")), row.names = c(NA, 
-10L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000023dd1999f50>)

summary of list

                                Length Class  Mode
all_views_20161003               16    tbl_df list
city_departments_in_current_bud   6    tbl_df list
crosswalk_to_budget_dept          2    tbl_df list
site_metrics                    285    tbl_df list
site_metrics_20161102           263    tbl_df list
table_metrics_ytd                30    tbl_df list

CodePudding user response:

With the current option, we may unlist the list elements and then stack

test <- stack(lapply(df_list, unlist))[2:1]

Or another option is

library(dplyr)
library(purrr)
map_dfr(df_list, ~ tibble(col2 = unlist(.x)), .id = 'col1')
  • Related