Examples

Working with dplyr

dplyr package (CRAN,GitHub) provides a group of functions that make data transformation much easier. These operators are fully compatible with dplyr and provide higher performance than its default pipe operator.

The following code demonstrates piping with dplyr functions.

library(dplyr)
# 
# Attaching package: 'dplyr'
# 
# The following objects are masked from 'package:stats':
# 
#     filter, lag
# 
# The following objects are masked from 'package:base':
# 
#     intersect, setdiff, setequal, union
library(pipeR)

mtcars %>>%
  select(mpg,cyl,disp,hp) %>>%
  filter(mpg <= median(mpg)) %>>%
  mutate(rmpg = mpg / max(mpg)) %>>%
  group_by(cyl) %>>%
  do(data.frame(mean=mean(.$rmpg),median=median(.$rmpg)))
# Source: local data frame [2 x 3]
# Groups: cyl
# 
#   cyl   mean median
# 1   6 0.9566 0.9427
# 2   8 0.7865 0.7917

. in the last call is introduced by do(...) rather than %>>%, which also makes it clear the scope of symbols.

Working with rlist

rlist package (CRAN,GitHub) is a set of tools for working with list objects storing non-tabular non-relational, or non-structured data as opposed to data frames. The major functions in this package are intentionally designed to work with pipeline.

The following example deals with the list of developers with names, ages, interests, and programming language they use and the number of years they have been using them.

library(rlist)
library(pipeR)
devs <- 
  list(
    p1=list(name="Ken",age=24,
      interest=c("reading","music","movies"),
      lang=list(r=2,csharp=4,python=3)),
    p2=list(name="James",age=25,
      interest=c("sports","music"),
      lang=list(r=3,java=2,cpp=5)),
    p3=list(name="Penny",age=24,
      interest=c("movies","reading"),
      lang=list(r=1,cpp=4,python=2)))

The following code queries the name of each developer who likes music and uses R, and put stack the results in a data frame.

devs %>>% 
  list.filter("music" %in% interest & "r" %in% names(lang)) %>>%
  list.select(name,age) %>>%
  list.stack
#    name age
# 1   Ken  24
# 2 James  25

The following example groups the developers by age and for each group it classifies the developers into interest classes and count the number of people in each class.

devs %>>%
  list.group(age) %>>%
  list.map(group -> group %>>%
      list.class(interest) %>>%
      list.map(int -> length(int))) %>>%
  str
# List of 2
#  $ 24:List of 3
#   ..$ movies : int 2
#   ..$ music  : int 1
#   ..$ reading: int 2
#  $ 25:List of 2
#   ..$ music : int 1
#   ..$ sports: int 1