Practice with Dplyr

This is a fake dataset about stocks

library(tidyverse)

# set the seed
set.seed(1)

stocks<-data.frame(
  time=as.Date('2009-01-01')+0:9,
  X=rnorm(n=10, mean=20, sd=1),
  Y=rnorm(n=10, mean=20, sd=2), 
  Z=rnorm(n=10, mean=20, sd=4)
)

stocks
##          time        X        Y        Z
## 1  2009-01-01 19.37355 23.02356 23.67591
## 2  2009-01-02 20.18364 20.77969 23.12855
## 3  2009-01-03 19.16437 18.75752 20.29826
## 4  2009-01-04 21.59528 15.57060 12.04259
## 5  2009-01-05 20.32951 22.24986 22.47930
## 6  2009-01-06 19.17953 19.91013 19.77549
## 7  2009-01-07 20.48743 19.96762 19.37682
## 8  2009-01-08 20.73832 21.88767 14.11699
## 9  2009-01-09 20.57578 21.64244 18.08740
## 10 2009-01-10 19.69461 21.18780 21.67177

Now I will gather the columns

# gather
stocksG<-stocks%>%
  gather(key=stock, value=price, -time)

head(stocksG)
##         time stock    price
## 1 2009-01-01     X 19.37355
## 2 2009-01-02     X 20.18364
## 3 2009-01-03     X 19.16437
## 4 2009-01-04     X 21.59528
## 5 2009-01-05     X 20.32951
## 6 2009-01-06     X 19.17953
ggplot(stocksG, aes(x=time, y=price, color=stock))+
  geom_line()