简体   繁体   English

如何创建以 R 中的另一列为条件的新列?

[英]How to create new column conditioned on another column in R?

Lets, say I have data:假设我有数据:

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 create a new column based on filer.df and if RR_Code is included in filter.df, its 1, otherwise 0.现在我想根据 filer.df 创建一个新列,如果 RR_Code 包含在 filter.df 中,则为 1,否则为 0。

filter.df <- c("848410", "848490", "850131", "850132")

Expected outcome:预期结果:

   RR_Code Model filter
1   848140    X1      0
2   848180    FG      0
3   848190    FD      0
4   848310    XR      0
5   848360    RT      0
6   848410    FG      1
7   848490    CV      1
8   850131    GH      1
9   850132    ER      1
10  850133    RF      0

We can do我们可以做的

df$filter <- +(df$RR_Code %in% filter.df)

df
#   RR_Code Model filter
#1   848140    X1      0
#2   848180    FG      0
#3   848190    FD      0
#4   848310    XR      0
#5   848360    RT      0
#6   848410    FG      1
#7   848490    CV      1
#8   850131    GH      1
#9   850132    ER      1
#10  850133    RF      0

There are several ways of doing it, my first intuition would be using case_when , from dplyr .有几种方法可以做到这一点,我的第一个直觉是使用case_when中的dplyr

df$new_column <- case_when(df$RR_Code %in% filter.df ~ 1,
                           TRUE ~ 0)

Which would return:哪个会返回:

       RR_Code Model new_column
1   848140    X1          0
2   848180    FG          0
3   848190    FD          0
4   848310    XR          0
5   848360    RT          0
6   848410    FG          1
7   848490    CV          1
8   850131    GH          1
9   850132    ER          1
10  850133    RF          0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据 R 中的条件列名称创建新的数据框列 - Create new data frame column based on conditioned column names in R R数据中的新条件列 - New conditioned column in R data 如何使用 dplyr 创建以 R 中其他两列出现为条件的新列? - How to create a new column conditioned on the occurrences of two other columns in R using dplyr? 从一个数据框的不同列创建一个新列,该条件以另一个数据框的另一列为条件 - Create a new column from different columns of one data frame conditioned on another column from another data frame 在 R 中创建一个新列,条件是不同列和不同行中的值 - Creating a new column in R conditioned on the values in a different column and different row 如何根据R中的另一列内容创建新列 - How to create new column based on another column's contents in R 根据R中的另一列创建一个新列 - Create a new column based on another column in R 如何基于R中的另一列创建具有多个值的新列 - How to create a new column with multiple values based on another column in R 如何创建一个新列,该列从 R 中的另一列连续求和? - How to create a new column that consecutively sums from another column in R? 如何创建具有以另一列中的条件为条件的唯一值的列? - How to create a column with unique values conditioned on a condition in an other column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM