PartitionBy {DescTools}R Documentation

PartitionBy Evaluates a Function Groupwise

Description

Split the vector x into partitions and apply the function to each partition separately. Computation restarts for each partition.
The logic is the same as the OLAP functions in SQL, e.g. SUM(x) OVER (PARTITION BY group).

Usage

PartitionBy(x, by, FUN, ...)

Arguments

x

an atomic object, typically a vector.

by

list of one or more factors, each of same length as X. The elements are coerced to factors by as.factor.

FUN

Function to apply for each factor level combination.

...

optional arguments to FUN: the Note section.

Details

This is more or less the same as the function ave, with the arguments organized a bit different.

Value

a vector with the same length as x containing the groupwise results of FUN.

Note

Optional arguments to FUN supplied by the ... argument are not divided into cells. It is therefore inappropriate for FUN to expect additional arguments with the same length as X.

Author(s)

Andri Signorell <andri@signorell.net>

See Also

ave, tapply

Examples

d.frm <- data.frame(x=rep(1:4,3), v=sample(x=1:3, size=12, replace=TRUE), 
                    g=gl(4,3,labels=letters[1:4]), m=gl(3,4,labels=LETTERS[1:3]))

# SQL-OLAP: sum() over (partition by g)
PartitionBy(d.frm$x, d.frm$g, FUN=sum)
PartitionBy(d.frm$x, FUN=sum)

# more than 1 grouping variables are organized as list as in tapply:
PartitionBy(d.frm$x, list(d.frm$g, d.frm$m), mean)

# count
d.frm$count <- PartitionBy(d.frm$x, d.frm$g, length)

# rank
d.frm$rank <- PartitionBy(d.frm$v, d.frm$g, rank)
d.frm$dense_rank <- PartitionBy(d.frm$v, d.frm$g, DenseRank)
d.frm$rank_desc <- PartitionBy(d.frm$x, d.frm$g, function(x) rank(-x))

# row_number
d.frm$row_number <- PartitionBy(d.frm$v, d.frm$g, function(x) order(x))
d.frm


[Package DescTools version 0.99.24 Index]