Learn R Programming

maditr (version 0.7.4)

dt_count: Additional useful functions

Description

  • dt_count calculates number of cases by groups, possibly weighted. dt_add_count adds number of cases to existing dataset.

  • dt_top_n returns top n rows from each group.

Usage

dt_count(data, ..., weight = NULL, sort = FALSE, name = "n")

dt_add_count(data, ..., weight = NULL, sort = FALSE, name = "n")

dt_top_n(data, n, by, order_by = NULL)

Arguments

data

data.table/data.frame data.frame will be automatically converted to data.table.

...

variables to group by.

weight

optional. Unquoted variable name. If provided result will be the sum of this variable by groups.

sort

logical. If TRUE result will be sorted in desending order by resulting variable.

name

character. Name of resulting variable.

n

numeric. number of top cases. If n is negative then bottom values will be returned.

by

list or vector of grouping variables

order_by

unquoted variable name by which result will be sorted. If not specified, defaults to the last variable in the dataset.

Value

data.table

Examples

Run this code
# NOT RUN {
data(mtcars)

# dt_count
dt_count(mtcars, am, vs)
dt_add_count(mtcars, am, vs, name = "am_vs")[] # [] for autoprinting

# dt_top_n
dt_top_n(mtcars, 2, by  = list(am, vs))
dt_top_n(mtcars, 2, order_by = mpg, by  = list(am, vs))
# }

Run the code above in your browser using DataLab