【问题标题】:How do I create a new column indicating whether certain other columns contain a given value?如何创建一个新列来指示某些其他列是否包含给定值?
【发布时间】:2019-12-29 16:32:04
【问题描述】:

我想在 data.frame 中添加一个新列,以指示对于每一行,数字“2”是否出现在某些其他列中。这是一个适用于小型 data.frame 的简单版本:

df <- data.frame(mycol.1 = 1:5,  mycol.2= 5:1, other.col = -2:2)
df$mycols.contain.two <- df$mycol.1 ==2 | df$mycol.2 ==2
df

  mycol.1 mycol.2 other.col mycols.contain.two
1       1       5        -2              FALSE
2       2       4        -1               TRUE
3       3       3         0              FALSE
4       4       2         1               TRUE
5       5       1         2              FALSE

现在假设 data.frame 有 50 列,我希望新列指示是否有任何以“mycol”开头的列在每行中包含“2”,而不必使用“|”符号 49 次。我假设使用starts_with() 有一个优雅的 dplyr 答案,但我无法弄清楚语法。

【问题讨论】:

  • df$new_col = rowSums(df[, 1:2] == 2) &gt; 0 您可以使用grep() 通过名称搜索仅选择某些列。
  • 如果您添加reproducible example,将更容易帮助/查找已在 SO 上的相关帖子

标签: r dplyr


【解决方案1】:

你可以这样做:

df <- data.frame(mycol.1 = 1:5,  mycol.2= 5:1, other.col = -2:2)
df$TYPE <- ifelse(rowSums(ifelse(sapply(df, function (x){x == 2}), 1, 0)) > 0 , "TRUE", "FALSE")

# > df
# mycol.1 mycol.2 other.col  TYPE
# 1       1       5        -2 FALSE
# 2       2       4        -1  TRUE
# 3       3       3         0 FALSE
# 4       4       2         1  TRUE
# 5       5       1         2  TRUE

【讨论】:

    【解决方案2】:

    您可以通过索引来实现。让我们获取mtcars 数据。

    head(mtcars)
    
                       mpg cyl disp  hp drat    wt  qsec vs am gear carb
    Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
    Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
    Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
    Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
    Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
    Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
    

    之后,我们可以索引任何列。假设我们对第 8 到 11 列感兴趣,

     mtcars$new <- rowSums(mtcars[,8:11]==2)>0
    

    给予,

                       mpg cyl disp  hp drat    wt  qsec vs am gear carb   new
    Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4 FALSE
    Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4 FALSE
    Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1 FALSE
    Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1 FALSE
    Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2 TRUE
    Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1 FALSE
    > 
    

    【讨论】:

      【解决方案3】:

      您可以使用简单的apply() 循环:

      df <- data.frame(mycol.1 = 1:5,  mycol.2= 5:1, other.col = -2:2)
      df$mycols.contain.two <- apply(df, 1, function(x){any(x == 2)})
      

      或者如果您只想检查前 3 列:

      df <- data.frame(mycol.1 = 1:5,  mycol.2= 5:1, other.col = -2:2)
      df$mycols.contain.two <- apply(df, 1, function(x){any(x[1:3] == 2)})
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-30
        • 2022-08-10
        • 1970-01-01
        • 1970-01-01
        • 2022-01-24
        相关资源
        最近更新 更多