-2

I have a variable (year) which is all 2007 and 2008s but I need to change it into a factor so it reads 0 and 1s. Is there a way to do this?

So it can be used in a general linear model

2 Answers2

0

Assuming there are only two years in your year variable, for example: c(2007, 2007, 2008, 2007, 2008), then:

year_as_factor <- factor(year, labels = c(0, 1))

will give you a vector of factors with values of 0 where year has 2007 and 1 where year has 2008.

0

You don't actually need to change the levels to use it as input into a glm model, casting it as a factor using factor(year) would be sufficient. If, however, you still want to change the levels you can do so as per the answer below. In general you can change the levels of a factor variable via a statement such as

levels(year) <- c(...)

where c(...) contains the new factor levels.

ags29
  • 2,621
  • 1
  • 8
  • 14