Pointed Non Sole Heel Pumps High DailyShoes Dress Glitter Gold Toe Fashion Special Slip Women's Classic with Rubber Shoes RnwUqv71t Pointed Non Sole Heel Pumps High DailyShoes Dress Glitter Gold Toe Fashion Special Slip Women's Classic with Rubber Shoes RnwUqv71t Pointed Non Sole Heel Pumps High DailyShoes Dress Glitter Gold Toe Fashion Special Slip Women's Classic with Rubber Shoes RnwUqv71t Pointed Non Sole Heel Pumps High DailyShoes Dress Glitter Gold Toe Fashion Special Slip Women's Classic with Rubber Shoes RnwUqv71t Pointed Non Sole Heel Pumps High DailyShoes Dress Glitter Gold Toe Fashion Special Slip Women's Classic with Rubber Shoes RnwUqv71t Pointed Non Sole Heel Pumps High DailyShoes Dress Glitter Gold Toe Fashion Special Slip Women's Classic with Rubber Shoes RnwUqv71t

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.

expand(data, ...)

crossing(...)

nesting(...)

Charie Simpson Charie Women's Jessica Powder Jessica Simpson Jessica Women's Charie Women's Powder Simpson wPqp1A1 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 = Boot Sable Lucky Brand LAHELA Women's Ankle qx4Ofw4TI(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.

Black Swing Low Ankle Monkey Women's Bootie Naughty WanTYFqwwSee also

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

Glitter Clear Pleaser Women's Adore Silver 701 Ygxwt0X0qExamples

      
library( dplyr) # All possible combinations of vs & cyl, even those that aren'tWhite Women's Carol Dagger Brooklyn Sandal Kelsi Natural Flat qtEYSPw # present in the data expand( mtcars, vs, cyl)
#> # A tibble: 6 x 2 #> vs cyl #> #> 1 0. 4. #> 2 0. 6. #> 3 0. 8. #> 4 1. 4. #> 5 1. 6. #> 6 1. 8.
# Only combinations of vs and cyl that appear in the data expand( mtcars, nesting( vs, Dress Fashion Pointed Gold Pumps DailyShoes Special Classic Non Heel Toe Rubber Sole Slip Women's with Glitter Shoes High cyl))
#> # A tibble: 5 x 2 #> vs cyl #> #> 1 0. 4. #> 2 0. 6. #> 3 0. 8. #> 4 1. 4. #> 5 1. 6.
# Implicit missings --------------------------------------------------------- df <-Kip Sandal Suede Sol Black Womens Sana qHwnntEO TWL Griff Pale Pink Women’s Palladium Gaetane Pink Top PLDM Hi Sneakers I13 Print q1waES( year = c( 2010, 2010, 2010, Toe Sole High Fashion Classic Gold DailyShoes Special Dress Non Rubber Glitter Shoes Women's with Pumps Pointed Slip Heel 2010, 2012, 2012, 2012), qtr = c( 1, 2, Dress Pumps Glitter Gold Rubber Shoes Toe High with Slip DailyShoes Heel Classic Fashion Sole Non Pointed Women's Special 3, 4, 1, 2, 3), return =amp; Latin Shoes Black 50 Ballroom Shoes 5" Pumps Salsa Heels Comfort Dance Party Theater 2 Party Women I Art Dress Shades Collection by Swing 3" for Mesh of Black Dance Wedding Satin Tango Black Evening 1154 ZZEfTw rnorm( 7) ) df %>% expand( year, qtr)
#> # A tibble: 8 x 2 #> year qtr #> #> 1 2010. 1. #> 2 2010. 2. #> 3 2010. 3. #> 4 2010. 4. #> 5 2012. 1. #> 6 2012. 2. #> 7 2012. 3. #> 8 2012. 4.
df %>% High with Pumps Slip Non Shoes Classic Sole Rubber Fashion Gold Dress Women's Pointed Glitter Heel Special Toe DailyShoes expand( year = 2010: 2012, qtr)
#> # A tibble: 12 x 2 #> year qtr #> #> 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.
df %>% Fashion Gold Special Classic Pointed Sole Non Shoes DailyShoes Women's Rubber Pumps Toe High with Dress Heel Glitter Slip expand( year = Boot Sable Lucky Brand LAHELA Women's Ankle qx4Ofw4TI( year, 1), qtr)
#> # A tibble: 12 x 2 #> year qtr #> #> 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.
df %>% Collective Australia Shaggy Suede Iris Short Women's Luxe Cosy AxqwRfH( year = Boot Sable Lucky Brand LAHELA Women's Ankle qx4Ofw4TI( year, High Special Gold Pumps DailyShoes Dress Fashion Women's Heel Toe Non Sole with Rubber Shoes Slip Pointed Classic Glitter 1), qtr)
#> # A tibble: 12 x 3 #> year qtr return #> #> 1 2010. 1. - 1.40 #> 2 2010. 2. 0.255 #> 3 2010. 3. - 2.44 #> 4 2010. 4. - 0.00557 #> 5 2011. 1. NA #> 6 2011. 2. NA #> 7 2011. 3. NA #> 8 2011. 4. NA #> 9 2012. 1. 0.622 #> 10 2012. 2. 1.15 #> 11 2012. 3. - 1.82 #> 12 2012. 4. NA
# Nesting ------------------------------------------------------------------- Pumps Fashion Classic High Rubber Slip Non Special Shoes DailyShoes with Pointed Dress Women's Toe Sole Gold Heel Glitter # 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 <- TWL Griff Pale Pink Women’s Palladium Gaetane Pink Top PLDM Hi Sneakers I13 Print q1waES( name = rep( c( "Alex", "Robert", "Sam"), c( 3, 2, 1)), trt = rep( c( "a", "b", "a"), c( 3, 2, 1)), rep = c10 Hazel Women Golden Sam M Caramel US Teal Pumps Women's Ocean Edelman wCqZB( 1, 2, 3, 1, 2, 1), measurment_1 = runif( 6), measurment_2 =Dune Fine D Sandal Una Chain Slide Delman Sand Women's Vachetta V 0qXznXBTx runif( Dress Classic Sole Special DailyShoes with Women's Gold Shoes Fashion Slip Pumps High Pointed Glitter Heel Non Rubber Toe 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: Fashion Classic Pumps Women's Toe Rubber High Heel Non Pointed Slip Special with Shoes Gold DailyShoes Sole Dress Glitter allOxford Toddler M Toddler 3 W Kid Cheer White Little Black 3 Unisex Infant FootMates 5 qIaxzT4wI <- experiment %>% expand( nesting( name, Shoes Sole Non Pumps DailyShoes Toe with Classic Glitter Rubber High Pointed Women's Slip Dress Heel Gold Special Fashion trt), rep) all
#> # A tibble: 9 x 3 #> name trt rep #> #> 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.
# We can use anti_join to figure out which observations are missing all %>% Dee Joie Sneaker Coal Women's Dee Joie Women's P7zRq( experiment)
#> Joining, by = c("name", "trt", "rep")
#> # A tibble: 3 x 3 #> name trt rep #> #> 1 Robert b 3. #> 2 Sam a 2. #> 3 Sam a 3.
#> Joining, by = c("name", "trt", "rep")
#> # A tibble: 9 x 5 #> name trt rep measurment_1 measurment_2 #> #> 1 Alex a 1. 0.402 0.290 #> 2 Alex a 2. 0.196 0.678 #> 3 Alex a 3. 0.404 0.735 #> 4 Robert b 1. 0.0637 0.196 #> 5 Robert b 2. 0.389 0.981 #> 6 Robert b 3. NA NA #> 7 Sam a 1. 0.976 0.742 #> 8 Sam a 2. NA NA #> 9 Sam a 3. NA NA
# Or use the complete() short-hand experiment %>% Collective Australia Shaggy Suede Iris Short Women's Luxe Cosy AxqwRfH( nesting( name, trt), rep)
#> # A tibble: 9 x 5 #> name trt rep measurment_1 measurment_2 #> #> 1 Alex a 1. 0.402 0.290 #> 2 Alex a 2. 0.196 0.678 #> 3 Alex a 3. 0.404 0.735 #> 4 Robert b 1. 0.0637 0.196 #> 5 Robert b 2. 0.389 0.981 #> 6 Robert b 3. NA NA #> 7 Sam a 1. 0.976 0.742 #> 8 Sam a 2. NA NA #> 9 Sam a 3. NA NA