Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reorganize list into dataframe using dplyr

I would convert a structured list in a tidy dataFrame using the speed of the dplyr package. I would know if the solution I am posting right now is "state-of-art" or there's something faster.

Here is an example of my starting list:

l = list()
l[[1]] = list(member1=c(a=rnorm(1)),member2=matrix(rnorm(3),nrow=3,ncol=1,dimnames=list(c(letters[2:4]),c("sample"))))
l[[2]] = list(member1=c(a=rnorm(1)),member2=matrix(rnorm(3),nrow=3,ncol=1,dimnames=list(c(letters[2:4]),c("sample"))))
l[[3]] = list(member1=c(a=rnorm(1)),member2=matrix(rnorm(3),nrow=3,ncol=1,dimnames=list(c(letters[2:4]),c("sample"))))

With this result (to show you the toy structure):

l
[[1]]
[[1]]$member1
    a 
0.3340196 

[[1]]$member2
 sample
b 1.0098830
c 0.6413375
d 0.9080675

[[2]]
[[2]]$member1
    a 
0.0590878 

[[2]]$member2
  sample
b  0.5585736
c -0.5936157
d -0.3985687

[[3]]
[[3]]$member1
     a 
0.06242458 

[[3]]$member2
  sample
b -0.2873391
c  0.5326067
d -1.1635551

Now I'll use a convenience function to rearrange the data an lapply to navigate through the list:

organizeSamples = function(x){
  member = x$member2
  output = data.frame(key=rownames(member),value=member[,1])
  return(output)
}
l_new = lapply(l, organizeSamples)

Now dplyr does the magic:

samples = dplyr::bind_rows(l_new)
samples :

  key      value
1   b  1.0098830
2   c  0.6413375
3   d  0.9080675
4   b  0.5585736
5   c -0.5936157 
6   d -0.3985687
7   b -0.2873391
8   c  0.5326067
9   d -1.1635551

There's a way to do it faster, more elegant & compact using dplyr?

like image 668
Fabio Avatar asked Mar 25 '15 20:03

Fabio


People also ask

How do I convert a list into a DataFrame in R?

Convert List to DataFrame using data. data. frame() is used to create a DataFrame in R that takes a list, vector, array, etc as arguments, Hence, we can pass a created list to the data. frame() function to convert list to DataFrame. It will store the elements in a single row in the DataFrame.

How do I rearrange data in R?

To sort a data frame in R, use the order( ) function. By default, sorting is ASCENDING. Prepend the sorting variable by a minus sign to indicate DESCENDING order.


1 Answers

Here's another approach with a little more dplyr/tidyr functions and piping, however I haven't tested its performance against the original approach in the question and whether it is more elegant depends on personal preference.

library(dplyr); library(tidyr)

lapply(l, `[[`, 2) %>% 
    data.frame %>% 
    add_rownames("key") %>% 
    gather(x, value, -key) %>% 
    select(-x)

#      key      value
#1       b -1.1476570
#2       c -0.2894616
#3       d -0.2992151
#4       b  0.2522234
#5       c -0.8919211
#6       d  0.4356833
#7       b -0.2242679
#8       c  0.3773956
#9       d  0.1333364
like image 139
talat Avatar answered Nov 09 '22 16:11

talat