The purrr package is a convenient tool for manipulating lists with functions. This assignment is a demonstration of how to use the functions in the purrr package.

Load the purrr library and the dplyr library as an assistant. We will use the college_all_ages data set from the fivethirtyeight package.

library(purrr)
library(fivethirtyeight)

First, let look at the first few rows of the college_all_ages data set

head(college_all_ages)
##   major_code                                 major
## 1       1100                   General Agriculture
## 2       1101 Agriculture Production And Management
## 3       1102                Agricultural Economics
## 4       1103                       Animal Sciences
## 5       1104                          Food Science
## 6       1105            Plant Science And Agronomy
##                    major_category  total employed
## 1 Agriculture & Natural Resources 128148    90245
## 2 Agriculture & Natural Resources  95326    76865
## 3 Agriculture & Natural Resources  33955    26321
## 4 Agriculture & Natural Resources 103549    81177
## 5 Agriculture & Natural Resources  24280    17281
## 6 Agriculture & Natural Resources  79409    63043
##   employed_fulltime_yearround unemployed unemployment_rate p25th median
## 1                       74078       2423        0.02614711 34000  50000
## 2                       64240       2266        0.02863606 36000  54000
## 3                       22810        821        0.03024832 40000  63000
## 4                       64937       3619        0.04267890 30000  46000
## 5                       12722        894        0.04918845 38500  62000
## 6                       51077       2070        0.03179089 35000  50000
##   p75th
## 1 80000
## 2 80000
## 3 98000
## 4 72000
## 5 90000
## 6 75000

Extract the unemployment_rate column as a list

major_unemp <- college_all_ages$unemployment_rate

The set_names function is used to assign a name to each of the element in a list.
In this example, I assign the names by their corresponding majors

major_unemp <- set_names(major_unemp,college_all_ages$major)
head(major_unemp)
##                   General Agriculture 
##                            0.02614711 
## Agriculture Production And Management 
##                            0.02863606 
##                Agricultural Economics 
##                            0.03024832 
##                       Animal Sciences 
##                            0.04267890 
##                          Food Science 
##                            0.04918845 
##            Plant Science And Agronomy 
##                            0.03179089

Section 1 - select particular elements from a list:
1.pluck() —- select an element by name or index.
For example: We can select the first element by

pluck(major_unemp, "General Agriculture")
## [1] 0.02614711
# or
pluck(major_unemp, 1)
## [1] 0.02614711

2.keep() —- select all elements that pass a logicl test.
For example: We can select all elements whose value is greater than 0.05

res <- keep(major_unemp, function(x) x > 0.05)
head(res)
##                         Soil Science                Environmental Science 
##                           0.05086705                           0.05128983 
##         Natural Resources Management                         Architecture 
##                           0.05434128                           0.08599113 
## Area Ethnic And Civilization Studies                       Communications 
##                           0.06793896                           0.06436031

3.discard() —- select all elements that DO NOT pass a logicl test.
For example: We can select all elements whose value is NOT greater than 0.05

res <- discard(major_unemp, function(x) x > 0.05)
head(res)
##                   General Agriculture 
##                            0.02614711 
## Agriculture Production And Management 
##                            0.02863606 
##                Agricultural Economics 
##                            0.03024832 
##                       Animal Sciences 
##                            0.04267890 
##                          Food Science 
##                            0.04918845 
##            Plant Science And Agronomy 
##                            0.03179089

4.head_while() —- select all elements from the beginning of a list up to and exclude the element that does not pass a logical test.
For example: we can select all elements whose value is greater than 0.02 from the beginning of a list

res <- head_while(major_unemp, function(x) x > 0.02)
head(res)
##                   General Agriculture 
##                            0.02614711 
## Agriculture Production And Management 
##                            0.02863606 
##                Agricultural Economics 
##                            0.03024832 
##                       Animal Sciences 
##                            0.04267890 
##                          Food Science 
##                            0.04918845 
##            Plant Science And Agronomy 
##                            0.03179089

5.tail_while() —- select all elements from the end of a list up to and exclude the element that does not pass a logical test.
For example: we can select all elements whose value is greater than 0.05 from the end of a list

res <- tail_while(major_unemp, function(x) x > 0.05)
head(res)
## Miscellaneous Business & Medical Administration 
##                                      0.05267856 
##                                         History 
##                                      0.06585101 
##                           United States History 
##                                      0.07349961

Section 2 - logical operations:
1.every() —- Check if all elements in a list pass a logical test
For example: we can check if all elements are greater than 0

every(major_unemp,function(x) x > 0)
## [1] FALSE

2.some() —- Check if some elements in a list pass a logical test
For example: we can check if some elements are 0

some(major_unemp,function(x) x == 0)
## [1] TRUE

3.some() —- Check if a value is an element of a list
For example: we can check if 0 is in the list or not

has_element(major_unemp,0)
## [1] TRUE

4.detect() —- Find the first element that passes a logical test
For example: we can find the first element that is greater than 0.08

detect(major_unemp,function(x) x > 0.08)
## [1] 0.08599113

5.detect_index() —- Find the index of the first element that passes a logical test
For example: we can find index of the first element that is greater than 0.08

detect_index(major_unemp,function(x) x > 0.08)
## [1] 12

Section 3 - applying function conditionally:
1.modify() —- Apply a function to each of the elements in a list
For example: multiply all values in the list by 100

res <- modify(major_unemp, function(x) x * 100)
head(res)
##                   General Agriculture 
##                              2.614711 
## Agriculture Production And Management 
##                              2.863606 
##                Agricultural Economics 
##                              3.024832 
##                       Animal Sciences 
##                              4.267890 
##                          Food Science 
##                              4.918845 
##            Plant Science And Agronomy 
##                              3.179089

2.modify_at() —- Apply a function to an element by name or index
For exmaple: multiply the first element by 100

res <- modify_at(major_unemp,1, function(x) x * 100)
head(res)
##                   General Agriculture 
##                            2.61471060 
## Agriculture Production And Management 
##                            0.02863606 
##                Agricultural Economics 
##                            0.03024832 
##                       Animal Sciences 
##                            0.04267890 
##                          Food Science 
##                            0.04918845 
##            Plant Science And Agronomy 
##                            0.03179089

3.modify_if() —- Apply a function to elements that pass a logical test
For exmaple: multiply the elements that are greater than 0.05 by 100

res <- modify_if(major_unemp, function(x) x > 0.05, function(x) x *100)
tail(res)
##                          International Business 
##                                      7.13537080 
##                          Hospitality Management 
##                                      5.14469830 
##   Management Information Systems And Statistics 
##                                      0.04397714 
## Miscellaneous Business & Medical Administration 
##                                      5.26785610 
##                                         History 
##                                      6.58510060 
##                           United States History 
##                                      7.34996100

Section 4 - lists combine:
1.append() —- Append another list to the end of a list
For example: Append the 3rd and 4th elements to the 1st and 2nd elements as a list

append(major_unemp[1:2], major_unemp[3:4])
##                   General Agriculture 
##                            0.02614711 
## Agriculture Production And Management 
##                            0.02863606 
##                Agricultural Economics 
##                            0.03024832 
##                       Animal Sciences 
##                            0.04267890

2.prepend() —- Add another list to the beginning of a list
For example: Add the 3rd and 4th elements to the beginning of the 1st and 2nd elements as a list

prepend(major_unemp[1:2], major_unemp[3:4])
##                Agricultural Economics 
##                            0.03024832 
##                       Animal Sciences 
##                            0.04267890 
##                   General Agriculture 
##                            0.02614711 
## Agriculture Production And Management 
##                            0.02863606

3.splice(): combine all lists and other elements into a list
For example: Combine the 1st and 2nd elements as a list, the 3rd and 4th elements as a list and a new element “0.123” into one list

splice(major_unemp[1:2], major_unemp[3:4], "new value" = "0.123")
## [[1]]
##                   General Agriculture 
##                            0.02614711 
## Agriculture Production And Management 
##                            0.02863606 
## 
## [[2]]
## Agricultural Economics        Animal Sciences 
##             0.03024832             0.04267890 
## 
## $`new value`
## [1] "0.123"

Other functions:

cross2() —- Using elements from 2 lists, create a list of all possible combinations with 1 element for the first list and another element from the second list
For example: create all combinations by using the first 2 values from the major column and the first 2 values from the unemployment rate column

cross2(college_all_ages$major[1:2],college_all_ages$unemployment_rate[1:2])
## [[1]]
## [[1]][[1]]
## [1] "General Agriculture"
## 
## [[1]][[2]]
## [1] 0.02614711
## 
## 
## [[2]]
## [[2]][[1]]
## [1] "Agriculture Production And Management"
## 
## [[2]][[2]]
## [1] 0.02614711
## 
## 
## [[3]]
## [[3]][[1]]
## [1] "General Agriculture"
## 
## [[3]][[2]]
## [1] 0.02863606
## 
## 
## [[4]]
## [[4]][[1]]
## [1] "Agriculture Production And Management"
## 
## [[4]][[2]]
## [1] 0.02863606

array_tree() —- Turn an array into a list

class(major_unemp)
## [1] "numeric"
res <- array_tree(major_unemp)
class(res)
## [1] "list"

map2() —- apply a function to each pair of elements from 2 lists and return the results as a list
For example: we combine 1 element from the major_category list and a corresponding element from the unemployment_rate list into a list. The result will be a list of 2-element lists.

major_unemp_cate <- map2(college_all_ages$major_category, college_all_ages$unemployment_rate, function(x,y) list(x,y))
major_unemp_cate <- set_names(major_unemp_cate,college_all_ages$major)
head(major_unemp_cate)
## $`General Agriculture`
## $`General Agriculture`[[1]]
## [1] "Agriculture & Natural Resources"
## 
## $`General Agriculture`[[2]]
## [1] 0.02614711
## 
## 
## $`Agriculture Production And Management`
## $`Agriculture Production And Management`[[1]]
## [1] "Agriculture & Natural Resources"
## 
## $`Agriculture Production And Management`[[2]]
## [1] 0.02863606
## 
## 
## $`Agricultural Economics`
## $`Agricultural Economics`[[1]]
## [1] "Agriculture & Natural Resources"
## 
## $`Agricultural Economics`[[2]]
## [1] 0.03024832
## 
## 
## $`Animal Sciences`
## $`Animal Sciences`[[1]]
## [1] "Agriculture & Natural Resources"
## 
## $`Animal Sciences`[[2]]
## [1] 0.0426789
## 
## 
## $`Food Science`
## $`Food Science`[[1]]
## [1] "Agriculture & Natural Resources"
## 
## $`Food Science`[[2]]
## [1] 0.04918845
## 
## 
## $`Plant Science And Agronomy`
## $`Plant Science And Agronomy`[[1]]
## [1] "Agriculture & Natural Resources"
## 
## $`Plant Science And Agronomy`[[2]]
## [1] 0.03179089

invoke_map() —- given a list of functions and a list of elements of the same length, apply each of the function to a corresponding element. For example: we can multiply the first element by 100 and multiply the second element by -1

res <- invoke_map(list(function(x) x * 100, function(y) y * -1), major_unemp[1:2])
head(res)
## [[1]]
## [1] 2.614711
## 
## [[2]]
## [1] -0.02863606

flatten() —- remove a level of indexes from a list
For example, we can flatten the just created list of 2-element lists into a single list

length(major_unemp_cate)
## [1] 173
res <-flatten(major_unemp_cate)
length(res)
## [1] 346

transpose() —- Transposes the index order in a multi-level list
For example, we can Transposes just created the list of 2-element lists

length(major_unemp_cate)
## [1] 173
res <- transpose(major_unemp_cate)
length(res)
## [1] 2

reduce() —- apply a functin recursively to each element of a list
For example: we can add up all elements in a list recursively

reduce(major_unemp, sum)
## [1] 9.922493

accumulate() —- the same as reduce(), but also return all intermediate results as a list

res <- accumulate(major_unemp, sum)
tail(res)
##                          International Business 
##                                        9.635040 
##                          Hospitality Management 
##                                        9.686487 
##   Management Information Systems And Statistics 
##                                        9.730464 
## Miscellaneous Business & Medical Administration 
##                                        9.783143 
##                                         History 
##                                        9.848994 
##                           United States History 
##                                        9.922493