Learn R Programming

expss (version 0.7.1)

tab_sort_asc: Partially (inside blocks) sort tables/data.frames

Description

tab_sort_asc/tab_sort_desc sort tables (usually result of cro/tables) in ascending/descending order between specified rows (by default, it is row which contain '#' in the first column). There is no non-standard evaluation in these functions by design so use quotes for names of your columns or use qc.

Usage

tab_sort_asc(x, columns = 2, excluded_rows = "#", na.last = FALSE)

tab_sort_desc(x, columns = 2, excluded_rows = "#", na.last = TRUE)

Arguments

x

data.frame

columns

character/numeric. Column names/numbers for data.frame/table by which object will be sorted. By default it is 2 - the first column with numbers in the table (there are row labels in the first column).

excluded_rows

character/logical/numeric rows which won't be sorted. Rows of the table will be sorted between excluded rows. If it is characters then they will be considered as pattern/vector of patterns. Patterns will be matched with Perl-style regular expression with values in the first column of x (see grep, perl = TRUE argument). Rows which have such patterns will be excluded. By default, pattern is "#" because "#" marks totals in the result of cro.

na.last

for controlling the treatment of NAs. If TRUE, missing values in the data are put last; if FALSE, they are put first; if NA, they are removed.

Value

sorted table('etable')/data.frame

Examples

Run this code
# NOT RUN {
data(mtcars)

# apply labels
mtcars = apply_labels(mtcars,
    cyl = "Number of cylinders",
    vs = "Engine",
    vs = c("V-engine" = 0,
                    "Straight engine" = 1),
    am = "Transmission",
    am = c(automatic = 0,
                    manual=1),
    gear = "Number of forward gears",
    carb = "Number of carburetors"
)

# without sorting
mtcars %>% calculate(cro_cpct(list(cyl, gear, carb), list("#total", vs, am)))

# with sorting
mtcars %>% 
    calculate(cro_cpct(list(cyl, gear, carb), list("#total", vs, am))) %>% 
    tab_sort_desc
# }

Run the code above in your browser using DataLab