indent_data_frame: Indents data.frames

View source: R/indent_data_frame.R

indent_data_frameR Documentation

Indents data.frames

Description

Indents data.frames for printing them as tables.

Usage

indent_data_frame(
  DD,
  keys,
  values = setdiff(colnames(DD), keys),
  character_empty = "",
  numeric_empty = NA,
  indent_character = "\\quad",
  colname_indent = "Group"
)

Arguments

DD

A data.frame. Should be sorted by keys with keys[1] varying slowest and keys[length(keys)] varying fastest.

keys

A character. Subset of colnames(DD) with length(keys)>=2. The combination of keys must be unique. DD[keys] must be class character or factor.

values

A character. Subset of colnames(DD). DD[keys] must be class character, factor or numeric.

character_empty

A character. Default ”. This character will be put in the new lines in class character columns.

numeric_empty

A numeric. Default NA. This character will be put in the new lines in class numeric columns.

indent_character

A character. character for one indent. Default is '\quad' (meant for latex). Can also be ' ' for Word.

colname_indent

A character. Default 'Group'. Name of the new column with the indented keys.

Details

Squeeze multiple key-columns into one column and indents the values accordingly. Adds new lines with the indented keys to the data.frame. Meant for wide tables that need to be narrower and more 'readable' Meant for plotting with e.g. xtable::xtable or Hmisc::latex or officer::body_add_table. Look at the examples for a more precise description. Meant for left-aligned columns. Thats why the indent_character is inserted to the left of the original values.

Value

A data.frame. Columns: c(colname_indent, values). Column colname_indent contains all combination of DD[keys], but now indented and squeezed in this column and casted to character. Columns 'values' contain all values of DD[values] unchanged. Number of rows is sum(cumprod(nlevels(DD[keys]))).

Examples


DD <- expand.grid(Arm = paste0('Arm ', c(1,2,4)),
                Gender = c('Male', 'Female'),
                Haircolor = c('Red', 'Green', 'Blue'),
                Income = c('Low', 'Med', 'High'), stringsAsFactors = TRUE)

DD <- doBy::orderBy(~ Arm + Gender + Haircolor + Income, DD)

DD$values1 <- runif(dim(DD)[1])
DD$values2 <- 1
DD$values3 <- sample(letters[1:4], size = nrow(DD), replace = TRUE)

keys = c('Arm', 'Gender', 'Haircolor', 'Income')
values = c('values1', 'values2', 'values3')
## Not run: 
DDD <- indent_data_frame(DD, keys, indent_character = '   ')

# print both:

Hmisc::latex(DD,
      file = '',
      longtable = TRUE,
      caption = 'Original table',
      rowname = NULL)

Hmisc::latex(DDD,
      file = '',
      longtable = TRUE,
      caption = 'Indented table',
      rowname = NULL)
      
## End(Not run)

atable documentation built on Sept. 11, 2024, 7:05 p.m.