# Choose factor level as dummy base in lm() in R

Lets say I am regressing Y on X1 and X2, where X1 is a numeric variable and X2 is a factor with four levels (A:D). Is there any way to write the linear regression function lm(Y ~ X1 + as.factor(X2)) so that I can choose a particular level of X2 -- say, B -- as the baseline?

-

You can use relevel() to change the baseline level of your factor. For instance,
> g <- gl(3, 2, labels=letters[1:3])