knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(dplyr)
library(lplyr)

Verbs for lists and pairlists

The package 'lplyr' extends some dplyr verbs to lists and pairlists:

xs <- list(x1 = 1:3, 
           x2 = 2:5, 
           x3 = "alpha")
mutate(xs, x4 = 4) %>% str
rename(xs, x0 = x1) %>% str

Usual verbs made for standard evaluation work as well:

mutate_(xs, x4 = ~ 4) %>% str
rename_(xs, x0 = ~ x1) %>% str

New verbs for data frames

The mutate_which and transmute_which functions are made for adding new variables or modifying existing ones on a subset of the data.

df <- mtcars[1:6,]
mutate_which(df, gear==4, carb = 100)
transmute_which(df, gear==4, carb = 100)

There is also a standard evaluation version of these functions, called mutate_which_ and transmute_which_:

mutate_which_(df, ~ gear==4, carb = ~ 100)
transmute_which_(df, ~ gear==4, carb = ~ 100)

The function pull selects a column in a data frame and transforms it into a vector. This is useful to use it in combination with magrittr's pipe operator and dplyr's verbs.

df[["mpg"]]
df %>% pull(mpg)

# more convenient than (mtcars %>% filter(mpg > 20))[[3L]]
df %>%
  filter(mpg > 20) %>%
  pull(3)


paulponcet/lplyr documentation built on May 24, 2019, 10:32 p.m.