I would like to get all possible combinations of all column values in a dataframe e.g.
library(tidyr)
# example data
fruits <- tibble(
type = c("apple", "orange", "banana"),
year = c(2010, 2011, 2012),
size = c("XS", "S", "M"),
weights = rnorm(3, 2)
)
# this works
expand(fruits, type, year, size, weights)
But in my real dataframe I want to expand over many columns and i) don't want to enter all column names manually and ii) won't always know the column names in advance. I was hoping this would work, but it doesn't.
expand(fruits, names(fruits))
Is there a way to use something like names()
, to automatically expand all columns?