OfficialBenWhite
OfficialBenWhite

Reputation: 141

For each unique entry in a list, add a range of dates in R?

So I have a list of unique values such as:

Ben
April
Joe

I want to create a data frame where for each entry in this list there is a date from a date range (1/1/2000 - 1/2/2000) entered

Ben    1/1/2000
Ben    1/2/2000
April  1/1/2000
April  1/2/2000
Joe    1/1/2000
Joe    1/2/2000    

I have tried doing this will lubridate and dplyr but I haven't had any luck

Upvotes: 0

Views: 128

Answers (1)

lukeA
lukeA

Reputation: 54237

Here is one approach:

vals <- c("Ben", "April", "Joe")
dateRange <- seq.Date(as.Date("2000-01-01"), as.Date("2000-01-03"), "1 day")
expand.grid(vals, dateRange)
# Var1       Var2
# 1   Ben 2000-01-01
# 2 April 2000-01-01
# 3   Joe 2000-01-01
# 4   Ben 2000-01-02
# 5 April 2000-01-02
# 6   Joe 2000-01-02
# 7   Ben 2000-01-03
# 8 April 2000-01-03
# 9   Joe 2000-01-03

Upvotes: 7

Related Questions