get a list of dates for the next 20 fridays in r

Here's a code snippet that shows how to get a list of dates for the next 20 Fridays in R using the lubridate package:

library(lubridate)

# Get the current date
today <- Sys.Date()

# Set the start date to the next Friday
start_date <- today + ceiling((5 - weekdays(today)) / 7) * 7

# Generate a sequence of 20 dates, incrementing by 7 days for each Friday
fridays <- seq(start_date, by = "week", length.out = 20)

# Format the dates as strings in the "YYYY-mm-dd" format
fridays_formatted <- format(fridays, "%Y-%m-%d")

# Print the list of dates
print(fridays_formatted)
456 chars
17 lines

This code will generate a list of the next 20 Fridays, starting from the current date. We use the Sys.Date() function to get today's date, then use the weekdays() function to determine the numeric day of the week (0-6, where 0 is Sunday and 6 is Saturday). We calculate the number of days until the next Friday, then use the ceiling() function to round up to the next whole number of weeks. We add this number of weeks (rounded up to the nearest Friday) to the current date to get the starting date.

We then use the seq() function to generate a sequence of 20 dates, incrementing by 7 days for each Friday. Finally, we format the dates as strings in the "YYYY-mm-dd" format using the format() function, and print the list of dates using print().

gistlibby LogSnag