fapply2: Apply formatting to two vectors

View source: R/fapply.R

fapply2R Documentation

Apply formatting to two vectors

Description

The fapply2 function applies formatting to two different vectors, and combines them into a single vector. This function is useful in cases where your data is in two different variables, and you would like them displayed as a single column for reporting purposes. For example, if you wish to create one column to display mean and standard deviation.

Usage

fapply2(
  x1,
  x2,
  format1 = NULL,
  format2 = NULL,
  sep = " ",
  width = NULL,
  justify = NULL
)

Arguments

x1

A vector, factor, or list to apply the format1 to.

x2

A second vector, factor, or list to which format2 will be applied.

format1

A format to be applied to the first input.

format2

A format to be applied to the second input.

sep

A separator to use between the two formatted values. Default is a single blank space (" ").

width

The desired character width of the formatted vector. Default value is NULL, meaning the vector will be variable width.

justify

Whether to justify the return vector. Valid values are 'left', 'right', 'center', 'centre', or 'none'.

Details

The fapply2 function works nearly the same as fapply. The difference is it has parameters for two vectors and formats instead of one. The output of the function is a single vector. The function essentially calls fapply on each vector and pastes them together afterwards.

There is an additional sep parameter so you can define a separator between the two formatted values. The width and justify parameters will apply to the single vector result. The function will also pick up format attributes on the supplied vectors.

The fapply2 function accepts any of the format types that fapply accepts. See fapply for additional information on the types of formats that can be applied.

Parameters may also be passed as attributes on the vector. See the fattr function for additional information on setting formatting attributes.

Value

A vector of formatted values.

See Also

fapply to format a single input, fcat to create a format catalog, value to define a format, fattr to easily set the formatting attributes of a vector, and flist to define a formatting list. Also see fdata to apply formats to an entire data frame, and FormattingStrings for how to define a formatting string.

Examples

# Create sample data
dt <- c(2.1, 5, 6, 9, 2, 7, 3)

# Calculate mean and standard deviation
v1 <- mean(dt)
v2 <- sd(dt)

# Apply formats and combine
fapply2(v1, v2, "%.1f", "(%.2f)")
# [1] "4.9 (2.66)"

dbosak01/fmtr documentation built on June 15, 2024, 4:26 a.m.