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)
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.
Equivalently, Pipe object can do the same thing.
Pipe(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
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
or equivalently using Pipe object:
Pipe(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.mapv(int -> length(int)))
# $`24`
# movies music reading
# 2 1 2
#
# $`25`
# music sports
# 1 1
or equivalently using Pipe object:
Pipe(devs)$
list.group(age)$
list.map(group -> Pipe(group)$
list.class(interest)$
list.mapv(int -> length(int))[]
)[]
# $`24`
# movies music reading
# 2 1 2
#
# $`25`
# music sports
# 1 1