spread_each: Spread multiple variables

Description Usage Arguments Value See Also Examples

View source: R/spread_each.R

Description

This is a multiple variable version of the function tidyr::spread().

Usage

1
2
spread_each(data, key, ..., fill = NA, convert = FALSE, drop = FALSE,
  sep = ".", key.first = TRUE)

Arguments

data

A data frame.

key

Column names or positions. This is passed to tidyselect::vars_pull().

These arguments are passed by expression and support quasiquotation (you can unquote column names or column positions).

...

the columns to act as the values to spread out.

fill

If set, missing values will be replaced with this value. Note that there are two types of missingness in the input: explicit missing values (i.e. NA), and implicit missings, rows that simply aren't present. Both types of missing value will be replaced by fill.

convert

If TRUE, type.convert() with asis = TRUE will be run on each of the new columns. This is useful if the value column was a mix of variables that was coerced to a string. If the class of the value column was factor or date, note that will not be true of the new columns that are produced, which are coerced to character before type conversion.

drop

If FALSE, will keep factor levels that don't appear in the data, filling in missing combinations with fill.

sep

the character to use to separate parts of column names.

key.first

If TRUE, the default, the columns are named {key level}{sep}{value column name}, otherwise the format is {value column name}{sep}{key level}{sep}

.

The separator between the key levels and the value column names.

Value

A wide tbl_df, with multiple value columns spread out.

See Also

Examples

1
2
3
4
5
6
7
8
library(dplyr)
data <- mutate(expand.grid( x = c( 'a', 'b', 'c')
                    , y = c( 'd', 'e', 'f')
                    , .rep = 1:10
                    ), v = rnorm(90))
long <- summarise(group_by(data, x, y),N=n(), sum=sum(v))

spread_each(long, y, N, sum)

tidymargins documentation built on July 23, 2019, 5:04 p.m.