Skip to contents

Converts tall data frames into wide data frames.

Usage

spread(data, key, value, fill = NA, convert = FALSE, drop = TRUE, sep = NULL)

Arguments

data

A data frame.

key

A character string for the name of the column which contains the values that will be 'spread' across as new column names.

value

A character string for the name of the column which contains the values that will be placed into the new columns.

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

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

If NULL, the column names will be taken from the values of key variable. If non-NULL, the column names will be given by <key_name><sep><key_value>.

Value

data.frame

References

Hadley Wickham and Lionel Henry (2017). tidyr: Easily Tidy Data with 'spread()' and 'gather()' Functions. R package version 0.8.1. https://CRAN.R-project.org/package=tidyr. Git commit 74bd48f781e793d2c1b85a93f216e595e910c5d9

Examples

#----------------------------------------------------------------------------
# spread() examples
#----------------------------------------------------------------------------
library(bkdat)

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

df1 <- gather_(data = stocks, key = "key", value = "value", cols = c("X", "Y", "Z"))
df2 <- df1
df2[30, 2] <- "ZZZ"

df1
#>          time ignore key        value
#> 1  2009-01-01      a   X  0.106684615
#> 2  2009-01-02      b   X -0.587013985
#> 3  2009-01-03      c   X -0.327853587
#> 4  2009-01-04      d   X -0.085361013
#> 5  2009-01-05      e   X -2.052403887
#> 6  2009-01-06      f   X  0.150748249
#> 7  2009-01-07      g   X -0.292872737
#> 8  2009-01-08      h   X  0.254997590
#> 9  2009-01-09      i   X -0.553238189
#> 10 2009-01-10      j   X  1.405108880
#> 11 2009-01-01      a   Y -1.590921899
#> 12 2009-01-02      b   Y -3.133028930
#> 13 2009-01-03      c   Y -2.081158222
#> 14 2009-01-04      d   Y  2.039867486
#> 15 2009-01-05      e   Y -1.404163956
#> 16 2009-01-06      f   Y  1.946631554
#> 17 2009-01-07      g   Y -0.153635305
#> 18 2009-01-08      h   Y  1.785849849
#> 19 2009-01-09      i   Y -1.555006177
#> 20 2009-01-10      j   Y  0.873594211
#> 21 2009-01-01      a   Z  1.653775739
#> 22 2009-01-02      b   Z  3.905367088
#> 23 2009-01-03      c   Z  4.586001996
#> 24 2009-01-04      d   Z  4.869086750
#> 25 2009-01-05      e   Z  0.001920052
#> 26 2009-01-06      f   Z  3.020500225
#> 27 2009-01-07      g   Z  1.369614042
#> 28 2009-01-08      h   Z  0.673891297
#> 29 2009-01-09      i   Z  5.588266035
#> 30 2009-01-10      j   Z -2.716381729
df2
#>          time ignore key        value
#> 1  2009-01-01      a   X  0.106684615
#> 2  2009-01-02      b   X -0.587013985
#> 3  2009-01-03      c   X -0.327853587
#> 4  2009-01-04      d   X -0.085361013
#> 5  2009-01-05      e   X -2.052403887
#> 6  2009-01-06      f   X  0.150748249
#> 7  2009-01-07      g   X -0.292872737
#> 8  2009-01-08      h   X  0.254997590
#> 9  2009-01-09      i   X -0.553238189
#> 10 2009-01-10      j   X  1.405108880
#> 11 2009-01-01      a   Y -1.590921899
#> 12 2009-01-02      b   Y -3.133028930
#> 13 2009-01-03      c   Y -2.081158222
#> 14 2009-01-04      d   Y  2.039867486
#> 15 2009-01-05      e   Y -1.404163956
#> 16 2009-01-06      f   Y  1.946631554
#> 17 2009-01-07      g   Y -0.153635305
#> 18 2009-01-08      h   Y  1.785849849
#> 19 2009-01-09      i   Y -1.555006177
#> 20 2009-01-10      j   Y  0.873594211
#> 21 2009-01-01      a   Z  1.653775739
#> 22 2009-01-02      b   Z  3.905367088
#> 23 2009-01-03      c   Z  4.586001996
#> 24 2009-01-04      d   Z  4.869086750
#> 25 2009-01-05      e   Z  0.001920052
#> 26 2009-01-06      f   Z  3.020500225
#> 27 2009-01-07      g   Z  1.369614042
#> 28 2009-01-08      h   Z  0.673891297
#> 29 2009-01-09      i   Z  5.588266035
#> 30 2009-01-10    ZZZ   Z -2.716381729

spread(data = df1, key = "key", value = "value")
#>          time ignore           X          Y            Z
#> 1  2009-01-01      a  0.10668461 -1.5909219  1.653775739
#> 2  2009-01-02      b -0.58701399 -3.1330289  3.905367088
#> 3  2009-01-03      c -0.32785359 -2.0811582  4.586001996
#> 4  2009-01-04      d -0.08536101  2.0398675  4.869086750
#> 5  2009-01-05      e -2.05240389 -1.4041640  0.001920052
#> 6  2009-01-06      f  0.15074825  1.9466316  3.020500225
#> 7  2009-01-07      g -0.29287274 -0.1536353  1.369614042
#> 8  2009-01-08      h  0.25499759  1.7858498  0.673891297
#> 9  2009-01-09      i -0.55323819 -1.5550062  5.588266035
#> 10 2009-01-10      j  1.40510888  0.8735942 -2.716381729
spread(data = df2, key = "key", value = "value")
#>          time ignore           X          Y            Z
#> 1  2009-01-01      a  0.10668461 -1.5909219  1.653775739
#> 2  2009-01-02      b -0.58701399 -3.1330289  3.905367088
#> 3  2009-01-03      c -0.32785359 -2.0811582  4.586001996
#> 4  2009-01-04      d -0.08536101  2.0398675  4.869086750
#> 5  2009-01-05      e -2.05240389 -1.4041640  0.001920052
#> 6  2009-01-06      f  0.15074825  1.9466316  3.020500225
#> 7  2009-01-07      g -0.29287274 -0.1536353  1.369614042
#> 8  2009-01-08      h  0.25499759  1.7858498  0.673891297
#> 9  2009-01-09      i -0.55323819 -1.5550062  5.588266035
#> 10 2009-01-10    ZZZ          NA         NA -2.716381729
#> 11 2009-01-10      j  1.40510888  0.8735942           NA