I have the following data frame in R, and I need to update the values in column feat_4
to 1 if any of the other 3 feature columns (feat_1, feat_2, and feat_3) contains a 1:
Name feat_1 feat_2 feat_3 feat_4
Tom 0 0 0 0
David 1 0 1 0
Tim 0 1 0 0
I know I can do this by using a for-loop
like this, but is there a more efficient/vectorized way of doing it? I could have more columns than what is being show above. Thank you for help!
for (row in 1:nrow(df)) {
if (any(df[row,2:4] == 1)) {
df$feat_4 = 1
}
}