fapply: Apply formatting to a vector

Description Usage Arguments Details Value Types of Formats See Also Examples

View source: R/fapply.R

Description

The fapply function applies formatting to a vector.

Usage

1
fapply(x, format = NULL, width = NULL, justify = NULL)

Arguments

x

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

format

A format to be applied.

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 fapply function accepts several types of formats: formatting strings, named vectors, vectorized functions, or user-defined formats. It also accepts a formatting list, composed of any of the previous types. The function will first determine the type of format, and then apply the format in the appropriate way. Results are returned as a vector.

The function also has parameters for width and justification.

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.

Types of Formats

The fapply function will process any of the following types of formats:

fapply will also accept a formatting list, which can contain any number of formats from the above list. To create a formatting list, see the flist function.

See Also

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

 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
## Example 1: Formatting string ##
v1 <- c(1.235, 8.363, 5.954, 2.465)

# Apply string format.
fapply(v1, "%.1f")


## Example 2: Named vector ##
# Set up vector
v2 <- c("A", "B", "C", "B")

# Set up named vector for formatting
fmt2 <- c(A = "Vector Label A", B = "Vector Label B", C = "Vector Label C")

# Apply format to vector
fapply(v2, fmt2)


## Example 3: User-defined format ##
# Define format
fmt3 <- value(condition(x == "A", "Format Label A"),
              condition(x == "B", "Format Label B"), 
              condition(TRUE, "Format Other"))
              
# Apply format to vector
fapply(v2, fmt3)


## Example 4: Formatting function ##
# Set up vectorized function
fmt4 <- Vectorize(function(x) {

  if (x %in% c("A", "B"))
    ret <- paste("Function Label", x)
  else
    ret <- "Function Other" 
    
  return(ret)
})

# Apply format to vector
fapply(v2, fmt4)


## Example 5: Formatting List - Row Type ##
# Set up data
# Notice each row has a different data type
v3 <- list(2841.258, "H", Sys.Date(),
           "L", Sys.Date() + 60, 1382.8865)
v4 <- c("int", "char", "date", "char", "date", "int")

# Create formatting list
lst <- flist(type = "row", lookup = v4,
       int = function(x) format(x, digits = 2, nsmall = 1, 
                                  big.mark=","),
       char = value(condition(x == "H", "High"),
                     condition(x == "L", "Low"),
                     condition(TRUE, "NA")),
       date = "%d%b%Y")

# Apply formatting list to vector
fapply(v3, lst)


## Example 6: Formatting List - Column Type ##
# Set up data
v5 <- c(Sys.Date(), Sys.Date() + 30, Sys.Date() + 60)
v5

# Create formatting list
lst <- flist("%B", "This month is: %s", type = "column")

# Apply formatting list to vector
fapply(v5, lst)

fmtr documentation built on July 25, 2021, 9:06 a.m.