Reputation: 335
I know it´s a very silly problem but I´m stuck.
I have a data frame (species) with a list of scientific names
structure(list(cientifico = structure(c(1L, 2L, 3L, 4L, 11L,
13L), levels = c("Capros aper", "Argentina sphyraena", "Arnoglossus imperialis",
"Arnoglossus laterna", "Blennius ocellaris", "Boops boops", "Buglossidium luteum",
"Callionymus lyra", "Callionymus maculatus", "Cepola macrophthalma",
"Chelidonichthys cuculus", "Chelidonichthys lucerna", "Chelidonichthys obscurus",
"Conger conger", "Eutrigla gurnardus", "Gadiculus argenteus",
"Gaidropsarus macrophthalmus", "Galeus melastomus", "Helicolenus dactylopterus",
"Lepidorhombus boscii", "Lepidorhombus whiffiagonis", "Trachinus draco", "Trachurus trachurus", "Zeus faber"), class = "factor")), row.names = c(NA,
6L), class = "data.frame")
and a list with small-sized species (small)
c("Argentina sphyraena", "Arnoglossus imperialis", "Arnoglossus laterna",
"Blennius ocellaris", "Buglossidium luteum", "Callionymus lyra")
I would like to add a new column in the data frame species based on the list small. If the species of the df is included in the list, the value in the new column is "pq", otherwise is "gr". I have playing with mutate, ifelse and %in% but without success.
Any hint will be more than welcome.
Upvotes: 1
Views: 40
Reputation: 12558
Something like the following ought to be what you are looking for:
dplyr::mutate(df, newcol = ifelse(cientifico %in% small, "pq", "br"))
Output:
cientifico newcol
1 Capros aper br
2 Argentina sphyraena pq
3 Arnoglossus imperialis pq
4 Arnoglossus laterna pq
5 Chelidonichthys cuculus br
6 Chelidonichthys obscurus br
Upvotes: 1