Suppose I have the date frame iris
, present in the memory of R:
head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
Suppose also that I also have a data frame named flores
, with the following structure:
flores <- data.frame(Especies=c("setosa", "virginica", "versicolor"),
Nome=c("Flor 1", "Flor 2", "Flor 3"))
Especies Nome
1 setosa Flor 1
2 virginica Flor 2
3 versicolor Flor 3
I'd like to replace occurrences of iris$Species
with flores$Nome
. That is, I would like every occurrence of setosa
in iris$Species
to be replaced with Flor 1
; each occurrence of virginica
in iris$Species
was replaced by Flor 2
; and each occurrence of versicolor
in iris$Species
was replaced with Flor 3
.
Using something like if
or ifelse
is out of the question, because the dataset I'm working with has thousands of occurrences of different species. It would be impossible to type all the options I have to work with.