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
In excel I know I can use
=IF((OR(F426=1,G426=1)),"1","0")
But I want to learn how to do it in R.
Thank you!
We can use |
df1$column3 <- with(df1, +(column1 == 1|column2 == 1))
Or with rowSums
df1$column3 <- +(rowSums(df1[c('column1', 'column2')] == 1) > 0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.