I have two data frames:
set.seed(002)
data1 <- data.frame(cbind(
a1 = sample(letters, 8, replace = TRUE),
a2 = rpois(8, 10)
), stringsAsFactors = FALSE)
data2 <- data.frame(cbind(
b1 = paste("area", 1:6, sep = " "),
b2 = c("e", "s", "o", "y", "d", "v")
), stringsAsFactors = FALSE)
data1
a1 a2
1 e 9
2 s 10
3 o 12
4 e 9
5 y 16
6 y 9
7 d 11
8 v 13
data2
b1 b2
1 area 1 e
2 area 2 s
3 area 3 o
4 area 4 y
5 area 5 d
6 area 6 v
I want to create a new column in data1 called a3 while matching a1 with information from data2 e.g if a1 = "e" then a3 = "area 1", if a1 = "d" then a3 = "area 5" and so on. The new data1 should look like this:
a1 a2 a3
1 e 9 area 1
2 s 10 area 2
3 o 12 area 3
4 e 9 area 1
5 y 16 area 4
6 y 9 area 4
7 d 11 area 5
8 v 13 area 6
I can achieve this by doing
data1 %>%
mutate(a3 = case_when(
a1 == "e" ~ "area 1",
a1 == "s" ~ "area 2",
a1 == "o" ~ "area 3",
a1 == "y" ~ "area 4",
a1 == "d" ~ "area 5",
TRUE ~ "area 6"
))
The problem is that I have many cases and I am to repeat this on a number of data frames with different cases.
I can do this with base r by writing
data1$a3 <- NA
for(i in 1:nrow(data2)){
for(j in 1:nrow(data1)){
if(data1[j,1] == data2[i,2]){
data1[j,3] <- data2[i,1]
}
}
}
but I am a fun of dplyr. Any assistance on how to achieve this using dplyr is appreciated.