bison2178
bison2178

Reputation: 789

r force a list to dataframe

I am trying to take a nested list and store the contents into a dataframe.

The list is an output from the Hmsic::describe function. Here is a test case.

  list <- Hmisc::describe(iris)

This list has multiple objects and some are nested. I am only interested in a subset of objects in the list.

unlist(list [[1]])[1:4]
unlist(list [[2]])[1:4]
unlist(list [[3]])[1:4]
unlist(list [[4]])[1:4]

The expected output will have two dataframes with the following list objects converted to columns

For list object based on continuous variable the expected dataframe will look as follows

   description               n       missing  distinct    lowest                       highest
   Sepal.Length            150        0            35     4.3, 4.4, 4.5, 4.6, 4.7      7.3, 7.4, 7.6, 7.7, 7.9
   Sepal.Width             150        0            23     2.0, 2.2, 2.3, 2.4, 2.5      3.9, 4.0, 4.1, 4.2, 4.4
   Petal.Length            150        0            43     1.0, 1.1, 1.2, 1.3, 1.4      6.3, 6.4, 6.6, 6.7, 6.9
   Petal.Width             150        0            22     0.1, 0.2, 0.3, 0.4, 0.5      2.1, 2.2, 2.3, 2.4, 2.5

For list object based on discrete variables the expected dataframe will look as follows

      description      n       missing   distinct   Values                           Frequency          
      Species        150        0        3          setosa, versicolor,  virginica   50,50,50

Any help on accomplishing this much appriciated. Thanks.

Upvotes: 0

Views: 186

Answers (1)

jhk0530
jhk0530

Reputation: 169

First of all. Hmsic has typo, it's Hmisc.

You can access list object's element with $.

and I don't think there's elegant function to make your dataframe.

Here's minimal example for continuous dataframe.

# install.packages('Hmisc')

listObj <- Hmisc::describe(iris)

dataframe <- c()

for(i in 1:4){
  subList <- listObj[[i]]

  rowadd <- c(
    subList$descript,
    subList$counts[['n']],
    subList$counts[['missing']],
    subList$counts[['distinct']],
    as.character(unname(paste(subList$extremes[1:5], collapse = ', '))),
    as.character(unname(paste(subList$extremes[6:10], collapse = ', ')))
  )

  dataframe <- rbind(dataframe, rowadd)

}
dataframe <- data.frame(dataframe, row.names = NULL)
colnames(dataframe) <- c('description', 'n', 'missing', 'distinct', 'lowest', 'highset')

Edit : R 4.0 supports now change list of variable to data.frame

Check list2DF() function if it works as you expected.

Upvotes: 1

Related Questions