vendredi 18 août 2017

Efficient way to code a for-loop over a dataframe by using if else statements in R

I have some troubles during my foor-loop. Furthermore I think this way to code is inefficient?

My expected outcome should be:
a new "class-variable", if different products matches "1" row-wise

My data.frame (df) consists of binary-coded data as follows:

Product1 Product2 ProductXX ProductXX
 0       1        1         1
 0       0        0         1
 1       0        1         1

for (x in 1:dim(df)){ if (Product20==1 | Product21==1 | Product22==1 | Product23==1) {df$newclass <- 1} else {df$newclass <- 0}}

Many thanks for your help!

Aucun commentaire:

Enregistrer un commentaire