Let's write a reproducible example. I will use data(mtcars)
.
This dataset has row names in each row:
row.names(mtcars)
[1] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive"
[5] "Hornet Sportabout" "Valiant" "Duster 360" "Merc 240D"
[9] "Merc 230" "Merc 280" "Merc 280C" "Merc 450SE"
[13] "Merc 450SL" "Merc 450SLC" "Cadillac Fleetwood" "Lincoln Continental"
[17] "Chrysler Imperial" "Fiat 128" "Honda Civic" "Toyota Corolla"
[21] "Toyota Corona" "Dodge Challenger" "AMC Javelin" "Camaro Z28"
[25] "Pontiac Firebird" "Fiat X1-9" "Porsche 914-2" "Lotus Europa"
[29] "Ford Pantera L" "Ferrari Dino" "Maserati Bora" "Volvo 142E"
Now I have another dataframe:
df2 <- structure(list(Cluster = c("Group 1", "Group 1", "Group 1", "Group 1",
"Group 1", "Group 1", "Group 1", "Group 1", "Group 2", "Group 2",
"Group 2", "Group 2", "Group 2", "Group 2", "Group 2")), row.names = c("Mazda RX4",
"Mazda RX4 Wag", "Datsun 710", "Hornet 4 Drive", "Hornet Sportabout",
"Valiant", "Duster 360", "Merc 240D", "Merc 230", "Merc 280",
"Merc 280C", "Merc 450SE", "Merc 450SL", "Merc 450SLC", "Cadillac Fleetwood"
), class = "data.frame")
df2
Cluster
Mazda RX4 Group 1
Mazda RX4 Wag Group 1
Datsun 710 Group 1
Hornet 4 Drive Group 1
Hornet Sportabout Group 1
Valiant Group 1
Duster 360 Group 1
Merc 240D Group 1
Merc 230 Group 2
Merc 280 Group 2
Merc 280C Group 2
Merc 450SE Group 2
Merc 450SL Group 2
Merc 450SLC Group 2
Cadillac Fleetwood Group 2
What I would like to do is to create a new column in the original mtcars
dataset (mtcars$Cluster
) with the information of the column df2$Cluster
, by following these rules:
- Search that the row name in
df2
is also present in the row name ofmtcars
. - If they are (same name in both datasets), introduce in
mtcars$Cluster
the value present indf2$Cluster
. - If they are not, skip that row and go to the next.
Be aware that this is an example, but my original dataframes:
- Some row names in
df2
might not be inmtcars
. - It will not happen that they are ordered between
mtcars
anddf2
.
Any idea?