How to include dummy variables for year? I have the following multiple linear regression:
reg <- lm(Y ~ x1 + x2 + d1 + d2, df)

and in my dataset I have a series called "year" which contains, you guessed it, a bunch of values representing the year in which that observation is recorded. I have about 20 unique year entries that don't span perfectly 1:1 (e.g. 1995 1996 then 1999; missing years). I want to find some sort of shorthand to including dummy variables for each year. 
I could use the -dummy_cols- function and one by one include them in the -lm- command but this is a hassle and I can't imagine not running into this same problem again. In Stata you can bypass this by creating dummy variables (i.e. y1 ... yn) and just write y* to include all variables beginning with y --- this is called a wildcard. Can't seem to find the equivalent in R and haven't found a close enough question on this page.
Please let me know in the comments if this should be migrated to Stack Overflow or there is already this question.
 A: All you have to do in R is to convert the year variable in your data into a factor:
df$year <- factor(df$year) 

Then have a look at the levels of this factor:
levels(df$year)

and make a mental note of which level is listed first by R. That level will be treated as the "reference" level against which all other levels will be compared when you include the year factor into your model.
Finally, include the factor year in your model. Say your model includes x1, x2 and year, so including year in this model amounts to: 
reg <- lm(Y ~ x1 + x2 + year, df)

This will prompt R to create dummy variables behind the scenes and include them in the model. In this sense, year in the above model call is just a shortcut for these dummy variables.
As an example, if your year variable includes k = 3 years (i.e., 1995, 1996 and 1999), R will create k - 1 = 2 dummy variables for inclusion in the model. These variables will be named year1996 and year1999 and will help you compare the mean value of Y between the years 1996 and 1995, and between the years 1999 and 1995 (all else being equal).  While these dummy variables are included implicitly in your model formula reg <- lm(Y ~ x1 + x2 + year, df), they are included explicitly in the model summary output obtained via summary(res).
You can change the reference level for your year factor with a command like this: 
df$year <- relevel(df$year, ref="1999") 

