Если вы хотите использовать решение dplyr, вы можете использовать mutate()
и case_when
:
library(tidyverse)
housingdata <-
data.frame(
stringsAsFactors = FALSE,
ID = c(1L, 2L, 3L, 4L, 5L, 6L, 7L),
City = c("Amsterdam","Berlin",
"Edinburgh","Stockholm","Amsterdam-Zuidoost","Chicago",
"Seattle")
)
housingdata_m <-
housingdata %>%
mutate(Category = case_when(City %in% c("Amsterdam", "Berlin", "Edinburgh", "Stockholm", "Amsterdam-Zuidoost") ~ City,
TRUE ~ "Other Cities"))