spread2: Spread without frustration

Description Usage Arguments Details Value See Also Examples

View source: R/spread2.R

Description

#' @description This is now deprecated and no longer needed due to advancements in 'tidyr'.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
spread2(
  data,
  key,
  value,
  compact = TRUE,
  fill = NA,
  convert = FALSE,
  drop = TRUE,
  sep = NULL
)

Arguments

data

See spread.

key

See spread.

value

See spread.

compact

When you don't have a unique identifier there is more than one way the output could be produced. Actually two. One is rather messy but may in fact be what is desired. The other is more 'compact'. See the examples.

fill

See spread.

convert

See spread.

drop

See spread.

sep

See spread.

Details

Anyone that uses spread more than once has likely come across the duplicate identifiers problem. This function works just like select (it's mostly just a wrapper) except that it works when you don't have unique identifiers, which is actually quite often. The only additional functionality regards the compact argument. See the examples for what this means.

For more, see StackOverflow for example, or the numerous repeated issue brought up at the tidyr GitHub issues page. Search for 'spread duplicate' for all, not just open issues (direct link couldn't be handled by R documentation).

Value

A data frame with 'wide' format.

See Also

spread

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
31
32
## Not run: 
library(tidyext); library(tidyr)

# initial example from spread
stocks_init <- 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)
)

# a very common situation
stocks <- data.frame(
  X = rnorm(10, 0, 1),
  Y = rnorm(10, 0, 2),
  Z = rnorm(10, 0, 4)
)


stocksm_init <- stocks_init %>% gather(stock, price, -time)
stocksm_init %>% spread(stock, price)    # works fine
stocksm <- stocks %>% gather(stock, price)
# stocksm %>% spread(stock, price)       # annoying
stocksm %>% spread2(stock, price)        # works fine
stocksm %>% spread2(stock, price, compact = FALSE)

# works with NA
stocksm$price[sample(1:nrow(stocksm), 5)] = NA
stocksm %>% spread2(stock, price)
stocksm %>% spread2(stock, price, compact = FALSE)

## End(Not run)

m-clark/tidyext documentation built on July 21, 2020, 2:36 a.m.