Less Volume, More Creativity with dplyr

R Pruim
2014 CVC Workshop

Less Volume, More Creativity with dplyr

5 commands

 mutate()     # add columns to data
 summarise()  # 1-row summary
 select()     # subset of columns
 filter()     # subset of rows
 arrange()    # put rows in desired order

Plus 1

  group_by()  # split/apply/combine

mutate()

require(mosaic); require(lubridate)
Births2 <- mutate(Births78, ldate=mdy(date))
head(Births2, 2)
    date births dayofyear      ldate
1 1/1/78   7701         1 2078-01-01
2 1/2/78   7527         2 2078-01-02
Births2 <- mutate(Births2, 
  ldate = mdy(date) - years(100),
  wday = wday(ldate, label = TRUE, abbr = TRUE))
head(Births2, 2)
    date births dayofyear      ldate wday
1 1/1/78   7701         1 1978-01-01  Sun
2 1/2/78   7527         2 1978-01-02  Mon

mutate()

xyplot( births ~ ldate, data = Births2,  
  groups = wday, type = 'l',
  auto.key = list(
    columns = 4, lines = TRUE, points = FALSE),
  par.settings = list(superpose.line=list(lty=1)))

plot of chunk unnamed-chunk-6

Explore Your Name

How has the rank of your name changed over time? Create a plot.

Explore Your Name

BabyNamesWithRank %>% 
  filter( name=="Randy" & sex=="M" ) -> Randy
xyplot( count ~ year, data = Randy, type="l")

plot of chunk unnamed-chunk-22

Explore Your Name

xyplot( rank ~ year, data = Randy,
        type="l", ylim=c(7000,0))

plot of chunk unnamed-chunk-23