create a dataframe with year from 2015 until 2023 in r

To create a dataframe with years from 2015 until 2023 in R, we can use the seq() function to generate a sequence of dates with the desired start and end dates, and then convert it to a data frame using the as.data.frame() function. Here's the code:

main.r
# Generate sequence of dates from 2015-01-01 to 2023-01-01
years <- seq(as.Date("2015-01-01"), as.Date("2023-01-01"), by = "year")

# Convert to data frame
df <- as.data.frame(years)

# Rename the column to "year"
colnames(df) <- "year"

# View the resulting data frame
df
273 chars
12 lines

This will result in a data frame with a single column named "year" and rows for each year from 2015 through 2023:

main.r
   year
1  2015
2  2016
3  2017
4  2018
5  2019
6  2020
7  2021
8  2022
9  2023
80 chars
11 lines

related categories

gistlibby LogSnag