Development on spread()
is complete, and for new code we recommend
switching to pivot_wider()
, which is easier to use, more featureful, and
still under active development.
df %>% spread(key, value)
is equivalent to
df %>% pivot_wider(names_from = key, values_from = value)
See more details in vignette("pivot")
.
spread(data, key, value, fill = NA, convert = FALSE, drop = TRUE, sep = NULL)
data | A data frame. |
---|---|
key, value | Column names or positions. This is passed to
These arguments are passed by expression and support quasiquotation (you can unquote column names or column positions). |
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. |
convert | If |
drop | If |
sep | If |
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 -2.0524039 -1.4041640 0.001920052 #> 2 2009-01-02 0.1507482 1.9466316 3.020500225 #> 3 2009-01-03 -0.2928727 -0.1536353 1.369614042 #> 4 2009-01-04 0.2549976 1.7858498 0.673891297 #> 5 2009-01-05 -0.5532382 -1.5550062 5.588266035 #> 6 2009-01-06 1.4051089 0.8735942 -2.716381729 #> 7 2009-01-07 -0.7954609 0.8268879 2.950517792 #> 8 2009-01-08 -1.5665145 1.9526835 -3.442897102 #> 9 2009-01-09 -1.0405791 2.2930010 1.684921683 #> 10 2009-01-10 1.0199337 2.4345434 5.802172853stocksm %>% spread(time, price)#> stock 2009-01-01 2009-01-02 2009-01-03 2009-01-04 2009-01-05 2009-01-06 #> 1 X -2.052403887 0.1507482 -0.2928727 0.2549976 -0.5532382 1.4051089 #> 2 Y -1.404163956 1.9466316 -0.1536353 1.7858498 -1.5550062 0.8735942 #> 3 Z 0.001920052 3.0205002 1.3696140 0.6738913 5.5882660 -2.7163817 #> 2009-01-07 2009-01-08 2009-01-09 2009-01-10 #> 1 -0.7954609 -1.566514 -1.040579 1.019934 #> 2 0.8268879 1.952684 2.293001 2.434543 #> 3 2.9505178 -3.442897 1.684922 5.802173# 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: chr "5.1" "7" #> $ Species : chr "setosa" "versicolor" #> $ Species_num : chr "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