swing: Apply a single function to multiple columns

Description Usage Arguments Details See Also Examples

Description

Usage

1
2
3
4
5
swing(.fun, ..., .tbl = get_tbl(), .name = "{var}",
  .env = caller_env())

twist(.fun, ..., .tbl = get_tbl(), .name = "{var}",
  .env = caller_env())

Arguments

.fun

A function or a formula that uses . or .x to refer to each of the selected column

...

tidy selection of columns, see tidyselect::vars_select() for details

.tbl, .env

data frame ... selects columns from, this is automatically set by the choreography(), you should rarely need to use these arguments

.name

glue::glue() model to name the outputs. The model may use :

  • {var} to refer to the name of the current selected variable

  • {idx} to refer to the index of the current variable The default value "var" for .name simply uses the name of the selected variable

Details

These functions are generally used within other dances, such as tango(), samba() or jive()

See Also

rumba() and zumba() to apply several functions to the same column

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
g <- iris %>% group_by(Species)

##------- tango()

# Apply mean to all columns that start with Sepal
# and choose how the result columns are called
g %>%
  tango(
    swing(mean, starts_with("Sepal"), .name = "mean_{var}")
  )

# if you want to use extra arguments of `.fun` you can embed
# them with the lambda syntax
g %>%
  tango(
    swing(~mean(., trim = .2), starts_with("Sepal"), .name = "mean_{var}")
  )

# use twist() to instead create a single packed column
g %>%
  tango(
    mean = twist(mean, starts_with("Sepal"))
  )
# but in fact, if you don't name the formula made by twist()
# the columns are auto spliced
g %>%
  tango(
    twist(mean, starts_with("Sepal"))
  )

##------- samba()

g %>%
  samba(
    swing(~. - mean(.), starts_with("Sepal"), .name = "centered_{var}")
  )

g %>%
  samba(
    centered = twist(~. - mean(.), starts_with("Sepal"), .name = "centered_{var}")
  )

##------- jive()

g %>%
  jive(
    q = ~ c("25%", "50%", "75%"),
    swing(~quantile(., c(0.25, 0.5, 0.75)), contains("."))
  )

romainfrancois/dance documentation built on May 14, 2019, 11:07 p.m.