tindex: Simple function to create intra-group ranking and count...

Description Usage Arguments Examples

View source: R/tindex.R

Description

This function creates an index or count variable by group, optionally sorting on an additional parameter of interest. For example, one may create an index/ranking of income within some group, e.g. the median income of census tracts within counties of a state. Or, one may create a static count within some grouping variable, e.g. class size within a dataframe of test scores.

Usage

1
tindex(df, grp, sortvar = NULL, output = "index", desc = FALSE)

Arguments

df

character; an R data.frame

grp

character; a grouping variable, such as a subject number, a classroom, a county, etc.

sortvar

character; sorts the data within grp (see above) by an additional variable (e.g., mpg within cyl in matcars). If no variable df is specified, data are sorted by grp.

output

character; what type of output is desired, a within group "index" (the default) or a "count" within grp?

desc

logical; when TRUE, sortvar is sorted in descending order within grp.

Examples

1
2
3
4
5
# Build a count variable by number of cylinders, returning a vector:
mtcars$cylinder_count <- tindex(mtcars, cyl, output="count")

# Build a within-type index sorting by a second variable:
mtcars$ranked <- tindex(mtcars, cyl, sortvar=mpg, output="index", desc=TRUE)

DerekYves/tindex documentation built on May 6, 2019, 2:10 p.m.