-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfmtr-fdata.Rmd
More file actions
75 lines (63 loc) · 1.9 KB
/
Copy pathfmtr-fdata.Rmd
File metadata and controls
75 lines (63 loc) · 1.9 KB
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
75
---
title: "Format Data Function"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Format Data Function}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
### How to use fdata()
Data can be formatted by assigning formats to the **format** attribute
of the columns in your dataframe or tibble, and then by calling the `fdata()`
function on that data. A sample program is as follows:
```{r eval=FALSE, echo=TRUE}
# Set up data frame
df <- mtcars[1:10, c("mpg", "cyl")]
df
# Define and assign formats
attr(df$mpg, "format") <- value(condition(x >= 20, "High"),
condition(x < 20, "Low"))
attr(df$cyl, "format") <- function(x) format(x, nsmall = 1)
# Apply formatting
fdata(df)
```
Here is the mtcars subset before formatting:
```
# mpg cyl
# Mazda RX4 21.0 6
# Mazda RX4 Wag 21.0 6
# Datsun 710 22.8 4
# Hornet 4 Drive 21.4 6
# Hornet Sportabout 18.7 8
# Valiant 18.1 6
# Duster 360 14.3 8
# Merc 240D 24.4 4
# Merc 230 22.8 4
# Merc 280 19.2 6
```
And here is the mtcars subset after formatting:
```
# mpg cyl
# Mazda RX4 High 6.0
# Mazda RX4 Wag High 6.0
# Datsun 710 High 4.0
# Hornet 4 Drive High 6.0
# Hornet Sportabout Low 8.0
# Valiant Low 6.0
# Duster 360 Low 8.0
# Merc 240D High 4.0
# Merc 230 High 4.0
# Merc 280 Low 6.0
```
You may apply formatting to variables of any data type: character, numeric,
date, etc. Internally, the `fdata()` function is using the `fapply()`
function on each column in the data frame. If there is no format assigned
to a column, that column is returned unaltered.
Next: [Format Apply Function](fmtr-fapply.html)