widen_columns: Create Multiple Sequentially Numbered Columns Containing A...

Description Usage Arguments Value Examples

View source: R/widen_columns.R

Description

Widen columns is intended to be used in a dplyr pipeline. Pass one or more columns containing discreet values to the widen columns function. Widen columns return a data frame with multiple sequentially numbered columns containing a single value for each value of each of the columns passed in. The names of the new sequentially numbered columns will be prefixed with the original column name and an underscore.

Typically, this function will be used on a grouped data frame.

Usage

1

Arguments

.data

a data frame (typically a grouped data frame)

...

a comma separated list of column names to widen

Value

a data frame

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
library(tidyverse)

df <- tibble(
  id = c(1, 1, 1, 1, 2),
  first = c("john", "john", "john", "john", "jane"),
  last = c("doe", "doe", "doe", "doe", "smith"),
  condition = c("diabetes", "diabetes", "mi", "mi", "depression"),
  meds = c("asprin", "tylenol", "asprin", "tylenol", "asprin")
) %>%
  print()

#>  # A tibble: 5 x 5
#>       id first last  condition  meds
#>    <dbl> <chr> <chr> <chr>      <chr>
#>  1     1 john  doe   diabetes   asprin
#>  2     1 john  doe   diabetes   tylenol
#>  3     1 john  doe   mi         asprin
#>  4     1 john  doe   mi         tylenol
#>  5     2 jane  smith depression asprin

df %>%
  group_by(id) %>%
  widen_columns(meds, condition)

#>  # A tibble: 2 x 7
#>  # Groups:   id [2]
#>       id first last  meds_1 meds_2  condition_1 condition_2
#>    <dbl> <chr> <chr> <chr>  <chr>   <chr>       <chr>
#>  1     1 john  doe   asprin tylenol diabetes    mi
#>  2     2 jane  smith asprin NA      depression  NA

brad-cannell/my_functions documentation built on July 25, 2019, 4:29 p.m.