1

I have a list of 100 observation units and a sequence of dates:

unit <- c(1:100)
date <- seq.Date(as.Date("2012-01-01"),as.Date("2012-12-01"), by = "month")

I want to create a new dataframe with each observational unit having a time unit. For example:

Unit     Date
 1    "2012-01-01"
 1    "2012-02-01"
 1    "2012-03-01"
 1    "2012-04-01"
 ................
 100   "2012-01-01"
 100   "2012-02-01"
 100   "2012-03-01"
 100   "2012-04-01"

2 Answers 2

3

You can use the following:

expand.grid(unit = unit, date = date)

If this is what you have in mind.

You can optionally order by unit as follows:

df <- expand.grid(unit = unit, date = date)
df <- df[order(df$unit), ]
Sign up to request clarification or add additional context in comments.

1 Comment

A simple answer. Works great. Thanks!
3

Not as concise as expand.grid, but more explicitly, you could do:

examp <- data.frame(Unit = rep(unit, each = length(date)), 
                    Date = rep(date, times = length(unit)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.