Spread a key-value pair across multiple columns.

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

Arguments

data

A data frame.

key

The bare (unquoted) name of the column whose values will be used as column headings.

value

The bare (unquoted) name of the column whose values will populate the cells.

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

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>".

See also

spread_ for a version that uses regular evaluation and is suitable for programming with.

Examples

library(dplyr) 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) ) stocksm <- stocks %>% gather(stock, price, -time) stocksm %>% spread(stock, price)
#> time X Y Z #> 1 2009-01-01 -0.25148344 0.03835518 -0.9835856 #> 2 2009-01-02 0.44479712 0.05912151 -4.7102532 #> 3 2009-01-03 2.75541758 1.09965508 -3.9034025 #> 4 2009-01-04 0.04653138 -4.54822971 4.2602293 #> 5 2009-01-05 0.57770907 5.36511437 0.5266825 #> 6 2009-01-06 0.11819487 -0.72244251 1.9545152 #> 7 2009-01-07 -1.91172049 0.42671150 -6.7978023 #> 8 2009-01-08 0.86208648 2.14869176 -5.8829452 #> 9 2009-01-09 -0.24323674 -1.33017650 1.1366014 #> 10 2009-01-10 -0.20608719 2.22790484 5.3492817
stocksm %>% spread(time, price)
#> stock 2009-01-01 2009-01-02 2009-01-03 2009-01-04 2009-01-05 2009-01-06 #> 1 X -0.25148344 0.44479712 2.755418 0.04653138 0.5777091 0.1181949 #> 2 Y 0.03835518 0.05912151 1.099655 -4.54822971 5.3651144 -0.7224425 #> 3 Z -0.98358565 -4.71025324 -3.903402 4.26022928 0.5266825 1.9545152 #> 2009-01-07 2009-01-08 2009-01-09 2009-01-10 #> 1 -1.9117205 0.8620865 -0.2432367 -0.2060872 #> 2 0.4267115 2.1486918 -1.3301765 2.2279048 #> 3 -6.7978023 -5.8829452 1.1366014 5.3492817
# Spread and gather are complements df <- data.frame(x = c("a", "b"), y = c(3, 4), z = c(5, 6)) df %>% spread(x, y) %>% gather(x, y, a:b, na.rm = TRUE)
#> z x y #> 1 5 a 3 #> 4 6 b 4
# Use 'convert = TRUE' to produce variables of mixed type df <- data.frame(row = rep(c(1, 51), each = 3), var = c("Sepal.Length", "Species", "Species_num"), value = c(5.1, "setosa", 1, 7.0, "versicolor", 2)) df %>% spread(var, value) %>% str
#> 'data.frame': 2 obs. of 4 variables: #> $ row : num 1 51 #> $ Sepal.Length: Factor w/ 6 levels "1","2","5.1",..: 3 4 #> $ Species : Factor w/ 6 levels "1","2","5.1",..: 5 6 #> $ Species_num : Factor w/ 6 levels "1","2","5.1",..: 1 2
df %>% spread(var, value, convert = TRUE) %>% str
#> 'data.frame': 2 obs. of 4 variables: #> $ row : num 1 51 #> $ Sepal.Length: num 5.1 7 #> $ Species : chr "setosa" "versicolor" #> $ Species_num : int 1 2