as_factor: Convert Select 'numform' Outputs to Factor

Description Usage Arguments Value Examples

View source: R/as_factor.R

Description

Convert month and weekday and weekday types to factor with correctly ordered levels. Note that the 'forcats' package imported by the 'tidyverse' package, has an as_factor function that can compete with numform's version. If in doubt, prefix with numform::as_factor.

Usage

1
as_factor(x, shift = 0, ...)

Arguments

x

A vector of weekdays or months.

shift

Shift the levels to the right or left. Useful for setting the week beginning to something besides Sunday. Use -1 to set to Monday instead.

...

ignored.

Value

Returns a factor vector with levels set.

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
dat <- structure(list(month1 = c("Jan", "Nov", "Mar", "Jul", "Aug",
"Jan", "Aug", "May", "Dec", "Apr"), month2 = c("March", "May",
"March", "July", "May", "October", "March", "November", "April",
"January"), weekday1 = c("Th", "F", "M", "Su", "Th", "Su", "M",
"Th", "W", "T"), weekday2 = c("We", "Th", "Fr", "Sa", "We", "Su",
"Tu", "Su", "Su", "Th"), weekday3 = c("Sat", "Wed", "Mon", "Wed",
"Wed", "Wed", "Wed", "Sun", "Fri", "Thu"), weekday4 = c("Sunday",
"Sunday", "Thursday", "Saturday", "Monday", "Wednesday", "Friday",
"Thursday", "Sunday", "Saturday")), .Names = c("month1", "month2",
"weekday1", "weekday2", "weekday3", "weekday4"))

## Note that the 'forcats' package imported by the 'tidyverse' package, has an
## `as_factor` function that can compete with numform's version.  If in doubt
## prefix with `numform::as_factor`
as_factor(dat$month1)
as_factor(dat$month2)
as_factor(dat$weekday1)
as_factor(dat$weekday2)
as_factor(dat$weekday3)
as_factor(dat$weekday4)

## shift levels
as_factor(dat$weekday4, -1)
as_factor(dat$weekday4, -2)
as_factor(dat$weekday4, 1)
as_factor(dat$weekday4, 2)

## Not run: 
library(tidyverse)

data_frame(
    revenue = rnorm(10000, 500000, 50000),
    date = sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 10000, TRUE),
    site = sample(paste("Site", 1:5), 10000, TRUE)
) %>%
    mutate(
        dollar = f_comma(f_dollar(revenue, digits = -3)),
        thous = f_thous(revenue),
        thous_dollars = f_thous(revenue, prefix = '$'),
        abb_month = f_month(date),
        abb_week = numform::as_factor(f_weekday(date, distinct = TRUE))
    ) %T>%
    print() %>%
    ggplot(aes(abb_week, revenue)) +
        geom_jitter(width = .2, height = 0, alpha = .2) +
        scale_y_continuous(label = ff_thous(prefix = '$'))+
        facet_wrap(~site) +
        theme_bw()

## End(Not run)

Example output

 [1] Jan Nov Mar Jul Aug Jan Aug May Dec Apr
Levels: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
 [1] March    May      March    July     May      October  March    November
 [9] April    January 
12 Levels: January February March April May June July August ... December
 [1] Th F  M  Su Th Su M  Th W  T 
Levels: Su M T W Th F S
 [1] We Th Fr Sa We Su Tu Su Su Th
Levels: Su Mo Tu We Th Fr Sa
 [1] Sat Wed Mon Wed Wed Wed Wed Sun Fri Thu
Levels: Sun Mon Tue Wed Thu Fri Sat
 [1] Sunday    Sunday    Thursday  Saturday  Monday    Wednesday Friday   
 [8] Thursday  Sunday    Saturday 
Levels: Sunday Monday Tuesday Wednesday Thursday Friday Saturday
 [1] Sunday    Sunday    Thursday  Saturday  Monday    Wednesday Friday   
 [8] Thursday  Sunday    Saturday 
Levels: Monday Tuesday Wednesday Thursday Friday Saturday Sunday
 [1] Sunday    Sunday    Thursday  Saturday  Monday    Wednesday Friday   
 [8] Thursday  Sunday    Saturday 
Levels: Tuesday Wednesday Thursday Friday Saturday Sunday Monday
 [1] Sunday    Sunday    Thursday  Saturday  Monday    Wednesday Friday   
 [8] Thursday  Sunday    Saturday 
Levels: Saturday Sunday Monday Tuesday Wednesday Thursday Friday
 [1] Sunday    Sunday    Thursday  Saturday  Monday    Wednesday Friday   
 [8] Thursday  Sunday    Saturday 
Levels: Friday Saturday Sunday Monday Tuesday Wednesday Thursday
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
✔ ggplot2 3.3.2purrr   0.3.4tibble  3.0.4dplyr   1.0.2tidyr   1.1.2stringr 1.4.0readr   1.4.0forcats 0.5.0
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ forcats::as_factor() masks numform::as_factor()dplyr::collapse()    masks numform::collapse()dplyr::filter()      masks stats::filter()dplyr::lag()         masks stats::lag()
# A tibble: 10,000 x 8
   revenue date       site   dollar   thous thous_dollars abb_month abb_week
     <dbl> <date>     <chr>  <chr>    <chr> <chr>         <chr>     <fct>   
 1 499256. 1999-10-02 Site 1 $499,000 499K  $499K         O         S       
 2 524782. 1999-06-13 Site 1 $525,000 525K  $525K         J         Su      
 3 409629. 1999-09-21 Site 4 $410,000 410K  $410K         S         T       
 4 482731. 1999-01-18 Site 1 $483,000 483K  $483K         J         M       
 5 482069. 1999-02-23 Site 4 $482,000 482K  $482K         F         T       
 6 467864. 1999-07-26 Site 4 $468,000 468K  $468K         J         M       
 7 521915. 1999-09-28 Site 1 $522,000 522K  $522K         S         T       
 8 550356. 1999-08-02 Site 4 $550,000 550K  $550K         A         M       
 9 456698. 1999-03-07 Site 4 $457,000 457K  $457K         M         Su      
10 442810. 1999-02-09 Site 3 $443,000 443K  $443K         F         T       
# … with 9,990 more rows
Warning message:
`data_frame()` is deprecated as of tibble 1.1.0.
Please use `tibble()` instead.
This warning is displayed once every 8 hours.
Call `lifecycle::last_warnings()` to see where this warning was generated. 

numform documentation built on Oct. 10, 2021, 1:10 a.m.