Может быть лучше создать пару ключ / значение list
, а затем извлечь элементы после замены, сопоставив '100 * * ключа
library(gsubfn)
# key val list
lst1 <- list(Paris = "City", `New York` = "City", Tokyo = "City",
`Washington DC` = "City",
Copenhagen = "City", Chicago = "City", Panama = "Country",
USA = "Country", Japan = "Country", Mexico = "Country", Israel = "Country",
Brazil = "Country", Asia = "Continent", Antarctica = "Continent",
Africa = "Continent", `North America` = "Continent",
`South America` = "Continent")
Извлеките совпадающие значения с помощью strapply
в list
, переберите list
с sapply
и paste
unique
строками, которые являются 'City', 'Continent' или 'Country'
nm1 <- c("City", "Continent", "Country")
df1$New <- sapply(strapply(df1$Words, "([^,]+)", lst1), function(x)
paste(unique(x[x %in% nm1]), collapse=","))
df1$New
#[1] "City,Continent" "Country,City,Continent"
#[3] "City,Country" "Country,Continent"
Данные
df1 <- structure(list(Color = c("red", "blue", "red", "blue"), Letter = c("A",
"A", "B", "B"), Words = c("Paris,Asia,parrot,Antarctica,North America,cat,lizard",
"Panama,New York,Africa,dog,Tokyo,Washington DC,fish",
"Copenhagen,bird,USA,Japan,Chicago,Mexico,insect",
"Israel,Antarctica,horse,South America,North America,turtle,Brazil"
)), class = "data.frame", row.names = c(NA, -4L))