statascii: Create Stata-like tables in the R console

Description Usage Arguments Value Examples

Description

Create Stata-like tables in the R console

Usage

1
statascii(df, ..., flavor = "oneway", padding = "stata", separators = FALSE)

Arguments

df

A data.frame or tibble.

...

A comma separated list of unquoted variable names. Use desc() to sort a variable in descending order.

flavor

Choose 'oneway', 'twoway', or 'summary'.

padding

Choose 'stata', 'summary', or 'none'.

separators

Tabbed row separators when tabulating more than one variable.

Value

A Stata-like formatted table is displayed in the console. An unformatted tibble is invisibly returned.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# setup
library(dplyr)
library(stringr)

# a. demonstrate 'oneway' flavor for one-way tables of frequencies
a <- mtcars %>% count(gear) %>% rename(Freq. = n)
a <- a %>% add_row(gear = "Total", Freq. = sum(a[, 2]))
statascii(a, flavor = "oneway")

# b. demonstrate 'oneway' flavor with no padding
b <- mtcars %>% count(gear) %>% rename(Freq. = n)
b <- b %>% add_row(gear = "Total", Freq. = sum(b[, 2]))
statascii(b, flavor = "oneway", padding = "none")

# c. demonstrate 'twoway' flavor for n-way tables of frequencies
c <- mtcars %>% count(gear, carb, am) %>% rename(Freq. = n)
c <- c %>% ungroup() %>% add_row(gear = "Total", carb = "", am = "", Freq. = sum(c[, 4]))
statascii(c, flavor = "twoway")

# d. demonstrate 'twoway' flavor with dashed group separator
d <- mtcars %>% count(gear, carb, am) %>% rename(Freq. = n)
d <- d %>% ungroup() %>% add_row(gear = "Total", carb = "", am = "", Freq. = sum(d[, 4]))
statascii(d, flavor = "twoway", separators = TRUE)

# e. demonstrate 'summary' flavor for summary statistics
e <- mtcars %>% group_by(gear) %>% summarize(
  Obs = n(),
  Mean = mean(gear),
  "Std. Dev." = sd(gear),
  Min = min(gear),
  Max = max(gear)
)
statascii(e, flavor = "summary")

# f. demonstrate wrapping feature for wide tables
f <- mtcars %>%
  mutate(cyl2 = cyl, vs2 = vs, am2 = am, carb2 = carb) %>%
  filter(gear != 5) %>%
  count(gear, carb, am, vs, cyl, carb2, am2, vs2, cyl2) %>%
  rename(Freq. = n) %>%
  ungroup()
f <- f %>% add_row(gear = "Total", Freq. = sum(f[, 10]))
f[is.na(f)] <- ""
statascii(f, flavor = "oneway", separators = TRUE)

gvelasq2/statascii documentation built on May 17, 2019, 9:30 a.m.