Я пытаюсь добавить новую переменную с именем "state_color" к набору данных "birth_data". Я хочу упростить мой код с помощью dplyr, но я не уверен, как его преобразовать. Я знаю, что в базе R это будет выглядеть так:
red <- c("AK","ID","KS","NE","ND","OK","UT","WY","TX","AL","MS","SC","MT","GA","MO","LA","TN","AK","KY","AZ","WV")
purple <- c("NC","VA","FL","OH","CO","NV","IN","IA","NM")
blue <- c("NH","PA","CA","MI","IL","MA","DE","NJ","CT","VT","ME","WA","OR","WI","NY","MA","RI","HI","MN","DC")
#assigning states to their respective color
birth_data$state_color[birth_data$state %in% red] <- "red"
birth_data$state_color[birth_data$state %in% purple] <- "purple"
birth_data$state_color[birth_data$state %in% blue] <- "blue"
head(birth_data)
Я пытаюсь сделать то же самое с dplyr
red <- c("AK","ID","KS","NE","ND","OK","UT","WY","TX","AL","MS","SC","MT","GA","MO","LA","TN","AK","KY","AZ","WV")
purple <- c("NC","VA","FL","OH","CO","NV","IN","IA","NM")
blue <- c("NH","PA","CA","MI","IL","MA","DE","NJ","CT","VT","ME","WA","OR","WI","NY","MA","RI","HI","MN","DC")
#assigning states to their respective color
birth_data %>%
mutate(state_color <- c("red","purple","blue"))
, но затем получаю ошибку
Ошибка: столбец state_color <- c("red", "purple", "blue")
должен иметь длину 1103629 (количество строк) или один, а не 3
Что я делаю не так?