serpentine: Perform serpentine sorts on multiple variables.

Description Usage Arguments Details Value Examples

Description

serpentine sorts data in a serpentine fashion (alternating between ascending and descending orders) for all variables specified. mixed_serpentine sorts the data with ascending or descending sorts for every variable specified except the last, which is serpentine sorted.

Usage

1
2
3

Arguments

data

is the data.frame to be sorted

...

are the variables to serpentine sort, in the given order. In serpentine, the first variable listed will be sorted in ascending order, the second variable will alternate between ascending and descending order by the value of the first variable, and so on. In mixed_serpentine, it is assumed all variables listed should be sorted in ascending order except the last, which is serpentine sorted. The user can choose a descending sort for any variable except the last by using the desc() wrapper.

Details

This is helpful in complex sampling designs with implicit stratification, as it reduces the variation in the stratified outcome for adjacent sampled units and thus reduces the overall sampling error. Serpentine sorts are commonly used in NCES surveys.

Value

A data.frame with equal size as the original data, but sorted differently.

Examples

1
2
3
4
5
6
7
8
9
# All variables except first are serpentine sorted
serpentine(data = mtcars, cyl, mpg)
serpentine(data = mtcars, cyl, vs, mpg)

# cyl, and vs are ascending sorted while mpg is serpentine sorted
mixed_serpentine(mtcars, cyl, vs, mpg)

# cyl is ascending, vs is descending, and mpg is serpentine sorted
mixed_serpentine(mtcars, cyl, desc(vs), mpg)

adamMaier/samplingTools documentation built on May 13, 2019, 1:16 p.m.