Pietro Fabbro
Pietro Fabbro

Reputation: 67

Columns as named list

After importing a Rdata object, I have a dataframe, in which the columns are stored as 'named list'. How do I unlist them?

structure(list(x1 = list(V1 = "1.", V2 = "2.", V3 = "3.", V4 = "4.", 
    V5 = "5."), company_name = list(V1 = "A", V2 = "B", 
    V3 = "C", 
    V4 = "D", V5 = "E"), 
    registered_office_address_commune = list(V1 = "Padova", V2 = "Padova", 
        V3 = "MISSING DATA", V4 = "MISSING DATA", V5 = "Padova")), row.names = c("V1", 
"V2", "V3", "V4", "V5"), class = "data.frame")

glimpse(df) gives

Rows: 5
Columns: 3
$ x1                                <named list> ["1.", "2.", "3.", "4.", "5."]
$ company_name                      <named list> ["A", "B", "C", "D", "E"]
$ registered_office_address_commune <named list> ["Padova", "Padova", "MISSING DATA", "MISSING DATA", "Padova"]

I would like to have a 'normal' dataframe, with columns saved either as numerical or character,

Can anyone help?

Upvotes: 2

Views: 44

Answers (2)

akrun
akrun

Reputation: 887841

We can use unnest

library(dplyr)
library(tidyr)
df %>%
   unnest
# A tibble: 5 x 3
#  x1    company_name registered_office_address_commune
#  <chr> <chr>        <chr>                            
#1 1.    A            Padova                           
#2 2.    B            Padova                           
#3 3.    C            MISSING DATA                     
#4 4.    D            MISSING DATA                     
#5 5.    E            Padova   

Upvotes: 0

nbenn
nbenn

Reputation: 691

dat <- structure(
  list(
    x1 = list(
      V1 = "1.", V2 = "2.", V3 = "3.", V4 = "4.", V5 = "5."
    ),
    company_name = list(
      V1 = "A", V2 = "B", V3 = "C", V4 = "D", V5 = "E"
    ), 
    registered_office_address_commune = list(
      V1 = "Padova", V2 = "Padova", V3 = "MISSING DATA",
      V4 = "MISSING DATA", V5 = "Padova"
    )
  ),
  row.names = c("V1", "V2", "V3", "V4", "V5"),
  class = "data.frame"
)

How do I unlist them?

You could iteratively call unlist() on columns. Using base R, you can do this as

lst <- lapply(df, unlist))

which gives you a list of atomic vectors, which then can be coerced to data.frame as

res <- as.data.frame(lst)

str(res)
#> 'data.frame':    5 obs. of  3 variables:
#>  $ x1                               : chr  "1." "2." "3." "4." ...
#>  $ company_name                     : chr  "A" "B" "C" "D" ...
#>  $ registered_office_address_commune: chr  "Padova" "Padova" "MISSING DATA" "MISSING DATA" ...

Upvotes: 1

Related Questions