Skip to contents

Converts wide data frames into tall data frames, i.e. converts columns into rows.

Usage

gather(.data, .key, .value, ...)

gather_(data, key, value, cols)

Arguments

.data, data

A data frame.

.key, key

A character string for the new key column name.

.value, value

A character string for the new value column name.

...

Unquoted column name separated by commas. Will be gathered and used to populate the key column.

cols

A character vector consisting of the column names that will be gathered and used to populate the key column.

Value

data.frame

Details

gather() does not check the types of columns you are gathering. It is the users responsibility to prevent unexpected type coercion.

Examples

#----------------------------------------------------------------------------
# gather() examples
#----------------------------------------------------------------------------
library(bkdat)

stocks <- data.frame(
  time = as.Date('2018-01-01') + 0:9,
  X = rnorm(10, 0, 1),
  Y = rnorm(10, 0, 2),
  Z = rnorm(10, 0, 4),
  letters = letters[1:10],
  stringsAsFactors = FALSE
)

gather_(data = stocks, key = "key", value = "value", cols = c("X", "Y", "Z"))
#>          time letters key        value
#> 1  2018-01-01       a   X -0.554064803
#> 2  2018-01-02       b   X  0.747176602
#> 3  2018-01-03       c   X -0.934819658
#> 4  2018-01-04       d   X -0.466620448
#> 5  2018-01-05       e   X -0.857190403
#> 6  2018-01-06       f   X -1.524744178
#> 7  2018-01-07       g   X  1.969424843
#> 8  2018-01-08       h   X  0.463174707
#> 9  2018-01-09       i   X -0.856152426
#> 10 2018-01-10       j   X  0.648043285
#> 11 2018-01-01       a   Y  0.151607920
#> 12 2018-01-02       b   Y  0.983522881
#> 13 2018-01-03       c   Y -1.507081412
#> 14 2018-01-04       d   Y  0.698054706
#> 15 2018-01-05       e   Y -0.341697944
#> 16 2018-01-06       f   Y  3.262412014
#> 17 2018-01-07       g   Y -1.565412083
#> 18 2018-01-08       h   Y -0.005787252
#> 19 2018-01-09       i   Y  0.826478585
#> 20 2018-01-10       j   Y  1.448866904
#> 21 2018-01-01       a   Z  9.415779083
#> 22 2018-01-02       b   Z -1.125798739
#> 23 2018-01-03       c   Z -1.924185745
#> 24 2018-01-04       d   Z  0.316903698
#> 25 2018-01-05       e   Z  3.079441321
#> 26 2018-01-06       f   Z  2.253347839
#> 27 2018-01-07       g   Z -1.495950095
#> 28 2018-01-08       h   Z -2.405222236
#> 29 2018-01-09       i   Z -1.696700139
#> 30 2018-01-10       j   Z -3.489263524
gather(.data = stocks, .key = "key", .value = "value", X, Y, Z)
#>          time letters key        value
#> 1  2018-01-01       a   X -0.554064803
#> 2  2018-01-02       b   X  0.747176602
#> 3  2018-01-03       c   X -0.934819658
#> 4  2018-01-04       d   X -0.466620448
#> 5  2018-01-05       e   X -0.857190403
#> 6  2018-01-06       f   X -1.524744178
#> 7  2018-01-07       g   X  1.969424843
#> 8  2018-01-08       h   X  0.463174707
#> 9  2018-01-09       i   X -0.856152426
#> 10 2018-01-10       j   X  0.648043285
#> 11 2018-01-01       a   Y  0.151607920
#> 12 2018-01-02       b   Y  0.983522881
#> 13 2018-01-03       c   Y -1.507081412
#> 14 2018-01-04       d   Y  0.698054706
#> 15 2018-01-05       e   Y -0.341697944
#> 16 2018-01-06       f   Y  3.262412014
#> 17 2018-01-07       g   Y -1.565412083
#> 18 2018-01-08       h   Y -0.005787252
#> 19 2018-01-09       i   Y  0.826478585
#> 20 2018-01-10       j   Y  1.448866904
#> 21 2018-01-01       a   Z  9.415779083
#> 22 2018-01-02       b   Z -1.125798739
#> 23 2018-01-03       c   Z -1.924185745
#> 24 2018-01-04       d   Z  0.316903698
#> 25 2018-01-05       e   Z  3.079441321
#> 26 2018-01-06       f   Z  2.253347839
#> 27 2018-01-07       g   Z -1.495950095
#> 28 2018-01-08       h   Z -2.405222236
#> 29 2018-01-09       i   Z -1.696700139
#> 30 2018-01-10       j   Z -3.489263524