My dataframe is the following:
df <- data.frame (RR_Code = c("848140", "848180", "848190", "848310", "848360", "848410", "848490", "850131", "850132", "850133"),
Model = c("X1", "FG", "FD", "XR", "RT", "FG", "CV", "GH", "ER", "RF"))
RR_Code Model
1 848140 X1
2 848180 FG
3 848190 FD
4 848310 XR
5 848360 RT
6 848410 FG
7 848490 CV
8 850131 GH
9 850132 ER
10 850133 RF
Now I want to add a category column according to these 4 dataframes below. If RR_Code match codes in R1 value in new column must be R1 and so on
R1 <- c("848140", "848180", "848190")
R2 <- c("848310", "848360", "848410")
R3 <- c("848490")
R4 <- c("850131", "850132", "850133")
Expected outcome:
RR_Code Model Category
1 848140 X1 R1
2 848180 FG R1
3 848190 FD R1
4 848310 XR R2
5 848360 RT R2
6 848410 FG R2
7 848490 CV R3
8 850131 GH R4
9 850132 ER R4
10 850133 RF R4
CodePudding user response:
You could create a list recording the relation of matches.
R.lst <- list(
R1 = c("848140", "848180", "848190"),
R2 = c("848310", "848360", "848410"),
R3 = c("848490"),
R4 = c("850131", "850132", "850133")
)
Then assign this named list to the levels of Category
:
df$Category <- factor(df$RR_Code)
levels(df$Category) <- R.lst
Another way is using fct_collapse()
from forcats
package to collapse factor levels into manually defined groups.
library(tidyverse)
df %>%
mutate(Category = fct_collapse(RR_Code, !!!R.lst))
RR_Code Model Category
1 848140 X1 R1
2 848180 FG R1
3 848190 FD R1
4 848310 XR R2
5 848360 RT R2
6 848410 FG R2
7 848490 CV R3
8 850131 GH R4
9 850132 ER R4
10 850133 RF R4
CodePudding user response:
You could also turn this into a join operation. Most of the work is just putting your R* vectors into a data.frame for the join.
library(dplyr)
tibble::lst(R1, R2, R3, R4) %>%
stack() %>%
rename(Categoyr=ind, RR_Code=values) %>%
right_join(df)
# RR_Code Categoyr Model
# 1 848140 R1 X1
# 2 848180 R1 FG
# 3 848190 R1 FD
# 4 848310 R2 XR
# 5 848360 R2 RT
# 6 848410 R2 FG
# 7 848490 R3 CV
# 8 850131 R4 GH
# 9 850132 R4 ER
# 10 850133 R4 RF