List grouping

rlist supports multiple types of grouping.

library(rlist)
devs <- 
  list(
    p1=list(name="Ken",age=24,
      interest=c("reading","music","movies"),
      lang=list(r=2,csharp=4,python=3)),
    p2=list(name="James",age=25,
      interest=c("sports","music"),
      lang=list(r=3,java=2,cpp=5)),
    p3=list(name="Penny",age=24,
      interest=c("movies","reading"),
      lang=list(r=1,cpp=4,python=2)))

list.group

list.group is used to classify the list members into different groups by evaluating a given expression and see what value it takes. The expression often produces a scalar value such as a logical value, a character value, and a number. Each group denotes a unique value that expression takes for at least one list member, and all members are put into one and only one group.

str(list.group(devs, age))
# List of 2
#  $ 24:List of 2
#   ..$ p1:List of 4
#   .. ..$ name    : chr "Ken"
#   .. ..$ age     : num 24
#   .. ..$ interest: chr [1:3] "reading" "music" "movies"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r     : num 2
#   .. .. ..$ csharp: num 4
#   .. .. ..$ python: num 3
#   ..$ p3:List of 4
#   .. ..$ name    : chr "Penny"
#   .. ..$ age     : num 24
#   .. ..$ interest: chr [1:2] "movies" "reading"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r     : num 1
#   .. .. ..$ cpp   : num 4
#   .. .. ..$ python: num 2
#  $ 25:List of 1
#   ..$ p2:List of 4
#   .. ..$ name    : chr "James"
#   .. ..$ age     : num 25
#   .. ..$ interest: chr [1:2] "sports" "music"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r   : num 3
#   .. .. ..$ java: num 2
#   .. .. ..$ cpp : num 5
str(list.group(devs, length(interest)))
# List of 2
#  $ 3:List of 1
#   ..$ p1:List of 4
#   .. ..$ name    : chr "Ken"
#   .. ..$ age     : num 24
#   .. ..$ interest: chr [1:3] "reading" "music" "movies"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r     : num 2
#   .. .. ..$ csharp: num 4
#   .. .. ..$ python: num 3
#  $ 2:List of 2
#   ..$ p2:List of 4
#   .. ..$ name    : chr "James"
#   .. ..$ age     : num 25
#   .. ..$ interest: chr [1:2] "sports" "music"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r   : num 3
#   .. .. ..$ java: num 2
#   .. .. ..$ cpp : num 5
#   ..$ p3:List of 4
#   .. ..$ name    : chr "Penny"
#   .. ..$ age     : num 24
#   .. ..$ interest: chr [1:2] "movies" "reading"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r     : num 1
#   .. .. ..$ cpp   : num 4
#   .. .. ..$ python: num 2

list.ungroup

list.group will produce a nested list in which the first level are groups and the second level are the original list members put into different groups.

list.ungroup is used to revert this process, or eradicate the group level of a list.

ageGroups <- list.group(devs, age)
str(list.ungroup(ageGroups))
# List of 3
#  $ p1:List of 4
#   ..$ name    : chr "Ken"
#   ..$ age     : num 24
#   ..$ interest: chr [1:3] "reading" "music" "movies"
#   ..$ lang    :List of 3
#   .. ..$ r     : num 2
#   .. ..$ csharp: num 4
#   .. ..$ python: num 3
#  $ p3:List of 4
#   ..$ name    : chr "Penny"
#   ..$ age     : num 24
#   ..$ interest: chr [1:2] "movies" "reading"
#   ..$ lang    :List of 3
#   .. ..$ r     : num 1
#   .. ..$ cpp   : num 4
#   .. ..$ python: num 2
#  $ p2:List of 4
#   ..$ name    : chr "James"
#   ..$ age     : num 25
#   ..$ interest: chr [1:2] "sports" "music"
#   ..$ lang    :List of 3
#   .. ..$ r   : num 3
#   .. ..$ java: num 2
#   .. ..$ cpp : num 5

list.cases

In non-relational data structures, a field can be a vector of multiple values. list.cases is used to find out all possible cases by evaluating a vector-value expression for each list member.

In data devs, field interest is usually a character vector of multiple values. The following code will find out all possible interests for all list members.

list.cases(devs, interest)
# [1] "movies"  "music"   "reading" "sports"

Or use similar code to find out all programming languages the developers use.

list.cases(devs, names(lang))
# [1] "cpp"    "csharp" "java"   "python" "r"

list.class

To group by cases, use list.class function. This function basically classify all list members case by case. Therefore, a long and nested list will be produces, in which the first-level denotes all the cases, and the second-level includes the original list members.

Note that each list member may belong to multiple cases, therefore the classification of the cases for each member is not exclusive. You may find one list member belong to multiple cases in the resulted list.

Case classification by interest:

str(list.class(devs, interest))
# List of 4
#  $ movies :List of 2
#   ..$ p1:List of 4
#   .. ..$ name    : chr "Ken"
#   .. ..$ age     : num 24
#   .. ..$ interest: chr [1:3] "reading" "music" "movies"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r     : num 2
#   .. .. ..$ csharp: num 4
#   .. .. ..$ python: num 3
#   ..$ p3:List of 4
#   .. ..$ name    : chr "Penny"
#   .. ..$ age     : num 24
#   .. ..$ interest: chr [1:2] "movies" "reading"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r     : num 1
#   .. .. ..$ cpp   : num 4
#   .. .. ..$ python: num 2
#  $ music  :List of 2
#   ..$ p1:List of 4
#   .. ..$ name    : chr "Ken"
#   .. ..$ age     : num 24
#   .. ..$ interest: chr [1:3] "reading" "music" "movies"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r     : num 2
#   .. .. ..$ csharp: num 4
#   .. .. ..$ python: num 3
#   ..$ p2:List of 4
#   .. ..$ name    : chr "James"
#   .. ..$ age     : num 25
#   .. ..$ interest: chr [1:2] "sports" "music"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r   : num 3
#   .. .. ..$ java: num 2
#   .. .. ..$ cpp : num 5
#  $ reading:List of 2
#   ..$ p1:List of 4
#   .. ..$ name    : chr "Ken"
#   .. ..$ age     : num 24
#   .. ..$ interest: chr [1:3] "reading" "music" "movies"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r     : num 2
#   .. .. ..$ csharp: num 4
#   .. .. ..$ python: num 3
#   ..$ p3:List of 4
#   .. ..$ name    : chr "Penny"
#   .. ..$ age     : num 24
#   .. ..$ interest: chr [1:2] "movies" "reading"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r     : num 1
#   .. .. ..$ cpp   : num 4
#   .. .. ..$ python: num 2
#  $ sports :List of 1
#   ..$ p2:List of 4
#   .. ..$ name    : chr "James"
#   .. ..$ age     : num 25
#   .. ..$ interest: chr [1:2] "sports" "music"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r   : num 3
#   .. .. ..$ java: num 2
#   .. .. ..$ cpp : num 5

Case classification by names(lang):

str(list.class(devs, names(lang)))
# List of 5
#  $ cpp   :List of 2
#   ..$ p2:List of 4
#   .. ..$ name    : chr "James"
#   .. ..$ age     : num 25
#   .. ..$ interest: chr [1:2] "sports" "music"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r   : num 3
#   .. .. ..$ java: num 2
#   .. .. ..$ cpp : num 5
#   ..$ p3:List of 4
#   .. ..$ name    : chr "Penny"
#   .. ..$ age     : num 24
#   .. ..$ interest: chr [1:2] "movies" "reading"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r     : num 1
#   .. .. ..$ cpp   : num 4
#   .. .. ..$ python: num 2
#  $ csharp:List of 1
#   ..$ p1:List of 4
#   .. ..$ name    : chr "Ken"
#   .. ..$ age     : num 24
#   .. ..$ interest: chr [1:3] "reading" "music" "movies"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r     : num 2
#   .. .. ..$ csharp: num 4
#   .. .. ..$ python: num 3
#  $ java  :List of 1
#   ..$ p2:List of 4
#   .. ..$ name    : chr "James"
#   .. ..$ age     : num 25
#   .. ..$ interest: chr [1:2] "sports" "music"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r   : num 3
#   .. .. ..$ java: num 2
#   .. .. ..$ cpp : num 5
#  $ python:List of 2
#   ..$ p1:List of 4
#   .. ..$ name    : chr "Ken"
#   .. ..$ age     : num 24
#   .. ..$ interest: chr [1:3] "reading" "music" "movies"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r     : num 2
#   .. .. ..$ csharp: num 4
#   .. .. ..$ python: num 3
#   ..$ p3:List of 4
#   .. ..$ name    : chr "Penny"
#   .. ..$ age     : num 24
#   .. ..$ interest: chr [1:2] "movies" "reading"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r     : num 1
#   .. .. ..$ cpp   : num 4
#   .. .. ..$ python: num 2
#  $ r     :List of 3
#   ..$ p1:List of 4
#   .. ..$ name    : chr "Ken"
#   .. ..$ age     : num 24
#   .. ..$ interest: chr [1:3] "reading" "music" "movies"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r     : num 2
#   .. .. ..$ csharp: num 4
#   .. .. ..$ python: num 3
#   ..$ p2:List of 4
#   .. ..$ name    : chr "James"
#   .. ..$ age     : num 25
#   .. ..$ interest: chr [1:2] "sports" "music"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r   : num 3
#   .. .. ..$ java: num 2
#   .. .. ..$ cpp : num 5
#   ..$ p3:List of 4
#   .. ..$ name    : chr "Penny"
#   .. ..$ age     : num 24
#   .. ..$ interest: chr [1:2] "movies" "reading"
#   .. ..$ lang    :List of 3
#   .. .. ..$ r     : num 1
#   .. .. ..$ cpp   : num 4
#   .. .. ..$ python: num 2

list.common

This function returns the common cases by evaluting a given expression for all list members.

Get the common interests of all developers.

list.common(devs, interest)
# character(0)

It concludes that no interest are common to every one.

Or get the common programming languages they all use.

list.common(devs, names(lang))
# [1] "r"

list.table

table function is often used to create a table with multiple dimensions. list.table is its counterpart that creates a table in which each dimension results from an expression.

list.table(devs, interests=length(interest), age)
#          age
# interests 24 25
#         2  1  1
#         3  1  0