dimanche 27 septembre 2020

How can I compare the values of different columns for each row?

So say I have a dataframe with a column for "play" and two columns with values:

df <- data.frame(Play = c("Comedy", "Midsummer", "Hamlet"),
                he = c(105, 20, 210),
                she = c(100, 23, 212)) 

I would like to get two vectors, one containing each Play with a higher value for "he" than "she", and one for the opposite, so each Play that has a higher value for "she" than "he".

I've looked at a few ways I thought going about it but none really seems to work, I tried building a 'if (x > y) {print z}' function then apply() that over my dataframe but I'm really far to inexperienced and run into so many problems, there ought to be simpler way than that …

Aucun commentaire:

Enregistrer un commentaire