serpentine: Perform serpentine sorts on multiple variables.

Description Usage Arguments Details Value Examples

View source: R/serpentine.R

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
serpentine(data = NULL, ..., random_num = 1)

mixed_serpentine(data = NULL, ...)

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.

random_num

is a random number to break ties randomly. This is most helpful when all variables on which the data is sorted are categorical as it is more liekly there are several rows of data with identical values on each category. Default is 1 so that results are reproducable.

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
10
11
12
13
14
15
16
17
# All variables except first are serpentine sorted
serpentine(data = mtcars, cyl, mpg)
serpentine(data = mtcars, cyl, vs, mpg)

# Same sort variables, but different resulting order because changing random number
serpentine(data = mtcars, vs, am)
serpentine(data = mtcars, vs, am, random_num = 5)

# Changing the random number has minimal effect when a non-cateogrical variable is included
serpentine(data = mtcars, cyl, vs, mpg)
serpentine(data = mtcars, cyl, vs, mpg, random_num = 23)

# 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, dplyr::desc(vs), mpg)

adamMaier/tntpmetrics documentation built on Feb. 1, 2022, 1:03 p.m.