mardi 10 avril 2018

Using If else for making a calculation in r?

I have a data frame in r and i have three columns S1, S2 and P

If s1 = 0 then i want to use s2 for a calculation and if s1 > 0 then i want to use s1 in the calculation.

I have tried using if else but keeps telling me i cant calculation operators in factors.

Whatt other way is their to do this?

My calc was

data$G <- ifelse(data$s1 == 0, data$s2 - data$p, data$s1 - data$p)

Would appreciate help on this I'm guessing it needs a different function to do this?

Aucun commentaire:

Enregistrer un commentaire