-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_1.Rmd
More file actions
187 lines (129 loc) · 4.13 KB
/
Copy pathexample_1.Rmd
File metadata and controls
187 lines (129 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
---
title: "purrr example_1"
author: "Kinare, Mayuresh"
date: "5/5/2018"
output:
html_document:
theme: united
highlight: tango
code_folding: show
---
```{r setup, include=FALSE, echo=TRUE}
#sourcing dependencies
# Obtain working directory
wd<- getwd()
source(paste0(wd,'/dependencies_library.R'))
```
## Using apply family vs purrr
This is an example from http://data.library.virginia.edu/getting-started-with-the-purrr-package-in-r/
***
Obtaining a list of names of the files we want to load
```{r}
# get all files ending in csv
files <- list.files(path = wd,pattern = "csv$")
files
```
***
Loading csv files using lapply
```{r}
# read in data normally
dat <- lapply(files, read.csv)
names(dat) <- gsub("\\.csv", "", files) # remove file extension
```
***
Loading csv files using purrr
```{r}
# read in data using purrr
dat2 <- map(files, read.csv)
names(dat2) <- gsub("\\.csv", "", files)
```
***
Computation using lapply
```{r}
#want to find the mean Open price for each stock. Here is a base R way using lapply and an anonymous function:
lapply(dat, function(x)mean(x$Open))
```
***
Computation using purrr
```{r}
#want to find the mean Open price for each stock.
map(dat, function(x)mean(x$Open))
```
***
purrr **map** allows us to bypass the function function. Using a tilda (~) in place of function and a dot (.) in place of x, we can do this:
```{r}
#Note that we are not using function as well as not passing x
map(dat, ~mean(.$Open))
```
***
Multiple types of map allow us to determine the structure of the return
```{r}
map_dbl(dat, ~mean(.$Open))
```
***
We want to extract each stock’s Open price data. In other words, we want to go into each data frame in our list and pull out the Open column. We can do that with lapply as follows:
```{r}
lapply(dat, function(x)x$Open)
```
***
Using purrr **map**
```{r}
map(dat, "Open")
```
***
We often want to plot financial data.
In this case we want to plot Closing price for each stock and look for trends.
We will do this with the base R function mapply.
1. Create a vector of stock names for plot labeling. 2. 2. Set up one row of three plotting regions.
3. Use mapply to create the plot.
**NOTE:** The “m” in mapply means “multiple arguments”. In this case we have two arguments: the Closing price and the stock name. Notice that mapply requires the function come first and then the arguments.
```{r, fig.asp=0.4}
#creating a list to use
stocks <- sub("\\.csv","", files)
par(mfrow=c(1,3))
mapply(function(x,y)plot(x$Close, type = "l", main = y), x = dat, y = stocks)
```
***
The purrr equivalent is map2.
```{r, fig.asp=0.4}
par(mfrow=c(1,3))
map2(dat, stocks, ~plot(.x$Close, type="l", main = .y))
```
***
If we want to not have the NULL returns from plot we can use **walk2**
```{r, fig.asp=0.4}
par(mfrow=c(1,3))
walk2(dat, stocks, ~plot(.x$Close, type="l", main = .y))
```
***
We want to now combine the 3 data sets into a single one and keep the meta data about which table it came from (which stock it was)
Using regular R,
```{r, results='asis'}
# we use do.call with rbind
datDF <- do.call(rbind, dat)
# add stock names to data frame
datDF$Stock <- gsub("\\.[0-9]*", "", rownames(datDF))
# remove period and numbers
#first 5 rows
knitr::kable(datDF[1:5,])
```
***
Using purrr, if use **map_df** we lose the information about which company stock it was
```{r, results='asis'}
dat2DFmdf <- map_df(files, read.csv) # works, but which record goes with which stock?
knitr::kable(dat2DFmdf[1:5,])
```
***
..or we use **reduce** we lose the information about which company stock it was
```{r, results='asis'}
dat2DFr <- reduce(dat, rbind) # works, but which record goes with which stock?
knitr::kable(dat2DFr[1:5,])
```
***
To accomplish adding a new column with the information about which companies stock it is, we use the stocks vector we created earlier along with the **map2_df** function and **update_list**
```{r, results='asis'}
dat2DFm2df <- map2_df(dat2, stocks, ~update_list(.x, Stock = .y))
knitr::kable(dat2DFm2df[1:5,])
```
***
###### This is a R Markdown document, more information on R Markdown https://rmarkdown.rstudio.com/lesson-1.html