library(rlist)

Problem

You know, a data.frame is a list. lists can be handled well with our Swiss army knife, rlist.

is(iris)
## [1] "data.frame" "list"       "oldClass"   "vector"

But, this ends up with this bloody error.

list.filter(iris, Species=="setosa")
#> Error in eval(expr, envir, enclos) : object 'Species' not found

Why??????????????

An example in rlist’s doc

x <- list(p1 = list(type='A',score=list(c1=10,c2=8)),
       p2 = list(type='B',score=list(c1=9,c2=9)),
       p3 = list(type='B',score=list(c1=9,c2=7)))
list.filter(x, type=='B')
## $p2
## $p2$type
## [1] "B"
## 
## $p2$score
## $p2$score$c1
## [1] 9
## 
## $p2$score$c2
## [1] 9
## 
## 
## 
## $p3
## $p3$type
## [1] "B"
## 
## $p3$score
## $p3$score$c1
## [1] 9
## 
## $p3$score$c2
## [1] 7

The structure of x is:

str(x)
## List of 3
##  $ p1:List of 2
##   ..$ type : chr "A"
##   ..$ score:List of 2
##   .. ..$ c1: num 10
##   .. ..$ c2: num 8
##  $ p2:List of 2
##   ..$ type : chr "B"
##   ..$ score:List of 2
##   .. ..$ c1: num 9
##   .. ..$ c2: num 9
##  $ p3:List of 2
##   ..$ type : chr "B"
##   ..$ score:List of 2
##   .. ..$ c1: num 9
##   .. ..$ c2: num 7

Whereas, that of iris is:

iris_list        <- iris
class(iris_list) <- "list"
str(iris_list)
## List of 5
##  $ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
##  - attr(*, "row.names")= int [1:150] 1 2 3 4 5 6 7 8 9 10 ...

x is row-wise and iris is column-wise.

zip and unzip

rlist has a function named list.zip(). Does this help?

str(list.zip(iris_list))
## List of 5
##  $ Sepal.Length:List of 1
##   ..$ iris_list: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width :List of 1
##   ..$ iris_list: num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length:List of 1
##   ..$ iris_list: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width :List of 1
##   ..$ iris_list: num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     :List of 1
##   ..$ iris_list: Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

No…

Welcome back to Hadleyverse!

Don’t worry, we have zip() in purrr!

library(purrr)
library(dplyr)
iris_zipped <- zip(iris)
str(iris_zipped[1:4])
## List of 4
##  $ :List of 5
##   ..$ Sepal.Length: num 5.1
##   ..$ Sepal.Width : num 3.5
##   ..$ Petal.Length: num 1.4
##   ..$ Petal.Width : num 0.2
##   ..$ Species     : int 1
##  $ :List of 5
##   ..$ Sepal.Length: num 4.9
##   ..$ Sepal.Width : num 3
##   ..$ Petal.Length: num 1.4
##   ..$ Petal.Width : num 0.2
##   ..$ Species     : int 1
##  $ :List of 5
##   ..$ Sepal.Length: num 4.7
##   ..$ Sepal.Width : num 3.2
##   ..$ Petal.Length: num 1.3
##   ..$ Petal.Width : num 0.2
##   ..$ Species     : int 1
##  $ :List of 5
##   ..$ Sepal.Length: num 4.6
##   ..$ Sepal.Width : num 3.1
##   ..$ Petal.Length: num 1.5
##   ..$ Petal.Width : num 0.2
##   ..$ Species     : int 1
# Species is 1, not "setosa", since factor is explicitly converted into numeric. Evil.
list.filter(iris_zipped, Species==1) %>% unzip()
## $Sepal.Length
##  [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4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