【问题标题】:How to add new column based on other columns如何根据其他列添加新列
【发布时间】:2019-09-29 08:27:38
【问题描述】:

我的数据在两个向量中包含 01 值:

  • bad = c(1,0,0,1,0,1,1)
  • good = c(1,0,0,0,1,0,0)

我想根据这两列创建新列。如果badgood 等于0(或1),那么新列中的观察必须是0(或1)或者如果bad1 good 是@ 987654336@ 那么新列中的新观察值应该是 1 ,反之亦然。

【问题讨论】:

  • 你能展示你的预期输出吗?另请阅读how to ask a good question 以及如何提供reproducible example
  • 有 4 种可能的组合,最好能看到每种组合的预期结果,vice versa 不清楚
  • 一个疯狂的猜测你可能需要像df$new <- ifelse(df$bad==df$good, df$good, df$bad)这样的东西。

标签: r


【解决方案1】:

可能是您要求使用其中一个逻辑运算符。

as.integer(bad | good)

as.integer(bad & good)

请参阅https://stat.ethz.ch/R-manual/R-devel/library/base/html/Logic.html 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-31
    • 2022-12-17
    • 1970-01-01
    • 2015-10-01
    • 2019-04-04
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多