This error is due to dat$var
being a factor. You can only assign values of pre-specified levels to a factor variable. But you can create the new variable with the following command (assuming option1
and option2
are the only values):
dat$var <- dat$var == "option1"