1

I have been looking around but I have been unable to find a way to do this in R.

I have multiple vectors and I want to combine them to create a fixed data structure.

For example I have the following vectors:

year <- c("2019","2020")
month <- c("1","2"....)
country <- c("USA","GER","CAN"...)

I want to be able to create a dataframe that is structured as follows:

year    month      country
2019      1           USA
2019      1           GER
2019      1           CAN
2019      2           USA
....

I have looked around, but I have not found a way to create the desired result.

Any help is greatly appreciated, Daniel

Daniel DE
  • 85
  • 1
  • 8

1 Answers1

2

An option is crossing from tidyr

library(tidyr)
crossing(year, month, country)

Or with expand.grid from base R

expand.grid(year, month, country)
akrun
  • 874,273
  • 37
  • 540
  • 662