1

I've been thinking really hard on this problem, and it can be boiled down to this:

I have the following two vectors (actually columns in two different dataframes):

 TIME <- 2000+1:3
 ID <- c("A", "B")

And I want this output

  ID2 <- c("A", "A", "A", "B", "B", "B")
  TIME2 <- c(2000+1:3, 2000+1:3)
  data.frame(ID2, TIME2)

The usual methods for combining datasets (I usually uses dplyr) assumes that there are shared values between the two datasets, or combines as seperate rows, and does not seem to fit the purpose. I also looked into creating this from within a for loop. (appending a row for each unique value and time in ID2 into a new dataframe, but I couldn't figure it out, and it seems to me like I'm stuck in a wrong way of thinking about this.

I guess that one approach might be to repeat each unique ID (length(TIME))-times into a vector, and cbind that with a vector of repeated TIME (length(unique(ID)).

Or maybe you have an elegant solution?

Jakn09ab
  • 179
  • 9

1 Answers1

0

Thanks to Ronak Shah and amrrs that both answered the question. Both solutions works.

 expand.grid(TIME, ID)
 tidyr::crossing(TIME,ID)
Jakn09ab
  • 179
  • 9