Chapter 6 Data Wrangling

6.1 Read csvs

6.1.1 I want to…

Read a list of csv and turn them all into a data frame.

6.1.3 Ok, but why?

map_df maps the read_csv on all the files, and returns a data.frame. The .id is used to keep track of the original file.

6.1.4 See also


6.2 Remove the NA

6.2.1 I want to…

Remove the NA from my list.

6.2.2 Here’s how to:

## [[1]]
## [1] 1
## 
## [[2]]
## [1] "a"
## 
## [[3]]
## [1] "b"

6.2.3 Ok, but why?

discard is the opposite of keep, and removes all the elements that validate a condition.

6.2.4 See also


6.3 List append

6.3.1 I want to…

Add an element at the end of all my sublists.

6.3.2 Here’s how to:

## [[1]]
## [[1]][[1]]
##  [1] 55  1  2  3  4  5  6  7  8  9 10
## 
## [[1]][[2]]
##  [1] 210   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16
## [18]  17  18  19  20
## 
## 
## [[2]]
## [[2]][[1]]
##  [1] 275  20  21  22  23  24  25  26  27  28  29  30
## 
## [[2]][[2]]
##  [1] 495  40  41  42  43  44  45  46  47  48  49  50

6.3.3 Ok, but why?

modify_depth allows to map function in sublists at a given depth.

prepend adds a element to the list at a givent index. If no index is given, the element is put at the beggining of the list.

6.3.4 See also


6.4 Position matching

6.4.1 I want to…

Find the first argument that matches a condition.

6.4.2 Here’s how to:

## [1] 60

6.4.3 Ok, but why?

detect_index returns the position of the first argument matching the condition. detect alone returns the value.

You can pass the .right argument to these function, so that the search starts from the end of the list.

6.4.4 See also


6.5 Cut a list

6.5.1 I want to…

Takes all the arguments from a list until the condition is met.

6.5.2 Here’s how to:

##  [1] 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
## [24] 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
## [47] 68 69 70 71 72 73 74 75 76 77 78 79 80

6.5.3 Ok, but why?

head_while, and its counterpart tail_while, take a list and perform a head or tail until a predicate is validated.

6.5.4 See also