0

The following is a simple example of what I am trying to do. I have a data frame which includes states as a variable. I want to create a new column that indicates whether or not its a coastal state and I have a vector of coastal states already made.

states<- c("AZ", "TN", "MI", "NY", "CA", "FL", "MA", "OH")
numbers<- c(1:8)
df<- data.frame(states, numbers)
coastal <- c("NY", "CA", "FL", "MA")

Ryan R
  • 1
  • Does this answer your question? [How to one hot encode several categorical variables in R](https://stackoverflow.com/questions/48649443/how-to-one-hot-encode-several-categorical-variables-in-r) – nikn8 May 08 '20 at 02:42

1 Answers1

1

Create a column called indicator with TRUE and FALSE:

df$indicator <- df$states %in% coastal

Create a column called indicator with 1 and 0:

df$indicator <- +(df$states %in% coastal)
HNSKD
  • 1,378
  • 1
  • 11
  • 22