expand: Expand data frame to include all combinations of values

Description Usage Arguments Details See Also Examples

View source: R/expand.R

Description

expand() is often useful in conjunction with left_join if you want to convert implicit missing values to explicit missing values. Or you can use it in conjunction with anti_join() to figure out which combinations are missing.

Usage

1
2
3
4
5

Arguments

data

A data frame.

...

Specification of columns to expand.

To find all unique combinations of x, y and z, including those not found in the data, supply each variable as a separate argument. To find only the combinations that occur in the data, use nest: expand(df, nesting(x, y, z)).

You can combine the two forms. For example, expand(df, nesting(school_id, student_id), date) would produce a row for every student for each date.

For factors, the full set of levels (not just those that appear in the data) are used. For continuous variables, you may need to fill in values that don't appear in the data: to do so use expressions like year = 2010:2020 or year = full_seq(year,1).

Length-zero (empty) elements are automatically dropped.

Details

crossing() is similar to expand.grid(), this never converts strings to factors, returns a tbl_df without additional attributes, and first factors vary slowest. nesting() is the complement to crossing(): it only keeps combinations of all variables that appear in the data.

See Also

complete() for a common application of expand: completing a data frame with missing combinations.

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
33
34
35
36
37
38
39
40
41
42
43
44
library(dplyr)
# All possible combinations of vs & cyl, even those that aren't
# present in the data
expand(mtcars, vs, cyl)

# Only combinations of vs and cyl that appear in the data
expand(mtcars, nesting(vs, cyl))

# Implicit missings ---------------------------------------------------------
df <- tibble(
  year   = c(2010, 2010, 2010, 2010, 2012, 2012, 2012),
  qtr    = c(   1,    2,    3,    4,    1,    2,    3),
  return = rnorm(7)
)
df %>% expand(year, qtr)
df %>% expand(year = 2010:2012, qtr)
df %>% expand(year = full_seq(year, 1), qtr)
df %>% complete(year = full_seq(year, 1), qtr)

# Nesting -------------------------------------------------------------------
# Each person was given one of two treatments, repeated three times
# But some of the replications haven't happened yet, so we have
# incomplete data:
experiment <- tibble(
  name = rep(c("Alex", "Robert", "Sam"), c(3, 2, 1)),
  trt  = rep(c("a", "b", "a"), c(3, 2, 1)),
  rep = c(1, 2, 3, 1, 2, 1),
  measurment_1 = runif(6),
  measurment_2 = runif(6)
)

# We can figure out the complete set of data with expand()
# Each person only gets one treatment, so we nest name and trt together:
all <- experiment %>% expand(nesting(name, trt), rep)
all

# We can use anti_join to figure out which observations are missing
all %>% anti_join(experiment)

# And use right_join to add in the appropriate missing values to the
# original data
experiment %>% right_join(all)
# Or use the complete() short-hand
experiment %>% complete(nesting(name, trt), rep)

Example output

Attaching package: 'dplyr'

The following objects are masked from 'package:stats':

    filter, lag

The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

# A tibble: 6 x 2
     vs   cyl
  <dbl> <dbl>
1     0     4
2     0     6
3     0     8
4     1     4
5     1     6
6     1     8
# A tibble: 5 x 2
     vs   cyl
  <dbl> <dbl>
1     0     4
2     0     6
3     0     8
4     1     4
5     1     6
# A tibble: 8 x 2
   year   qtr
  <dbl> <dbl>
1  2010     1
2  2010     2
3  2010     3
4  2010     4
5  2012     1
6  2012     2
7  2012     3
8  2012     4
# A tibble: 12 x 2
    year   qtr
   <int> <dbl>
 1  2010     1
 2  2010     2
 3  2010     3
 4  2010     4
 5  2011     1
 6  2011     2
 7  2011     3
 8  2011     4
 9  2012     1
10  2012     2
11  2012     3
12  2012     4
# A tibble: 12 x 2
    year   qtr
   <dbl> <dbl>
 1  2010     1
 2  2010     2
 3  2010     3
 4  2010     4
 5  2011     1
 6  2011     2
 7  2011     3
 8  2011     4
 9  2012     1
10  2012     2
11  2012     3
12  2012     4
# A tibble: 12 x 3
    year   qtr       return
   <dbl> <dbl>        <dbl>
 1  2010     1  0.005374752
 2  2010     2 -1.498914075
 3  2010     3  0.468752465
 4  2010     4  1.016844145
 5  2011     1           NA
 6  2011     2           NA
 7  2011     3           NA
 8  2011     4           NA
 9  2012     1  0.611587277
10  2012     2  1.164966504
11  2012     3 -0.808670516
12  2012     4           NA
# A tibble: 9 x 3
    name   trt   rep
   <chr> <chr> <dbl>
1   Alex     a     1
2   Alex     a     2
3   Alex     a     3
4 Robert     b     1
5 Robert     b     2
6 Robert     b     3
7    Sam     a     1
8    Sam     a     2
9    Sam     a     3
Joining, by = c("name", "trt", "rep")
# A tibble: 3 x 3
    name   trt   rep
   <chr> <chr> <dbl>
1    Sam     a     3
2    Sam     a     2
3 Robert     b     3
Joining, by = c("name", "trt", "rep")
# A tibble: 9 x 5
    name   trt   rep measurment_1 measurment_2
   <chr> <chr> <dbl>        <dbl>        <dbl>
1   Alex     a     1   0.60517607    0.7503468
2   Alex     a     2   0.96155291    0.1635574
3   Alex     a     3   0.61847852    0.6518150
4 Robert     b     1   0.07285942    0.9932899
5 Robert     b     2   0.33302704    0.8465653
6 Robert     b     3           NA           NA
7    Sam     a     1   0.06201630    0.9932282
8    Sam     a     2           NA           NA
9    Sam     a     3           NA           NA
# A tibble: 9 x 5
    name   trt   rep measurment_1 measurment_2
   <chr> <chr> <dbl>        <dbl>        <dbl>
1   Alex     a     1   0.60517607    0.7503468
2   Alex     a     2   0.96155291    0.1635574
3   Alex     a     3   0.61847852    0.6518150
4 Robert     b     1   0.07285942    0.9932899
5 Robert     b     2   0.33302704    0.8465653
6 Robert     b     3           NA           NA
7    Sam     a     1   0.06201630    0.9932282
8    Sam     a     2           NA           NA
9    Sam     a     3           NA           NA

tidyr documentation built on Oct. 29, 2018, 1:04 a.m.