[英]How to create categorical column condition on values rows in R?
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.现在我想根据下面这 4 个数据框添加一个类别列。 If RR_Code match codes in R1 value in new column must be R1 and so on
如果 RR_Code 匹配新列中 R1 值中的代码必须是 R1 等等
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
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
:然后将此命名列表分配给
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.另一种方法是使用
forcats
包中的fct_collapse()
将因子级别折叠到手动定义的组中。
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
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.
大部分工作只是将您的 R* 向量放入用于连接的 data.frame 中。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.