# create a vector of years to loop through years <- 2022:2030 # initialize an empty list to store the results for each year results <- list() # loop through each year and perform calculations on the subset of data for that year for (year in years) { # create a subset of the data for the current year year_data <- subset(my_data, format(datetime_col, "%Y") == as.character(year)) # perform calculations on the subset of data for the current year # for example, calculate the mean of the value column year_mean <- mean(year_data$value_col) # store the results for the current year in the results list results[[as.character(year)]] <- year_mean } # view the results for each year results