[英]Third column based on two other columns
What code can I use if I want to add a third column based on two other columns.如果我想基于另外两列添加第三列,我可以使用什么代码。
If column 1 OR column 2 are =1, I want column 3 to say 1如果第 1 列或第 2 列 = 1,我希望第 3 列说 1
In excel I know I can use在 excel 我知道我可以使用
=IF((OR(F426=1,G426=1)),"1","0") =IF((OR(F426=1,G426=1)),"1","0")
But I want to learn how to do it in R.但我想在 R 中学习如何做到这一点。
Thank you!谢谢!
We can use |
我们可以使用
|
df1$column3 <- with(df1, +(column1 == 1|column2 == 1))
Or with rowSums
或使用
rowSums
df1$column3 <- +(rowSums(df1[c('column1', 'column2')] == 1) > 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.