PaulaSpinola
PaulaSpinola

Reputation: 531

How can I create a dataframe combining all values of different variables (R)?

I would like to create a dataframe with id, year and month columns with all combinations of the values below.

id <- c(1002,463,546,9787,849,34)
years <- 2000:2009
months <- 1:12

In this simple example, the total number of rows of the dataframe I am looking to get should have 6*10*12 rows. Each one of the 6 ids should show up in 10*12 rows, each one referring to a given year-month.

Upvotes: 0

Views: 18

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 102469

Try expand.grid

expand.grid(id,years,months)

Upvotes: 1

Related Questions