Reputation: 47
I am trying to create a dummy variable in R where the categorical variable "position" equals 1 for 'first base' or 'second base'. I know how to do this with only one like 'first base' but I'm not sure how to do it including second base as option. How would I write this code?
baseball$POS <- ifelse(baseballdf$position == "first base", 1, 0)
Upvotes: 0
Views: 1632
Reputation: 887213
We can use %in%
instead of ==
if we are matching multiple elements and also, ifelse
is not needed as TRUE/FALSE
can be coerced to 1/0 with as.integer
or +
as.integer(baseballdf$position %in% c("first base", "second base"))
Upvotes: 1