dimanche 25 novembre 2018

Applying ifelse function to transform entire column in data frame

I have a data frame such as this:

df <- data.frame(x = c("A", "B", "C", "D"),
                 y = c(-1, -2, 3, 1))

And I want to multiply the values of df$y by -1 if the value of A is less than 0, as is the case here. So instead of being (-1, -2, 3, 1), it becomes (1, 2, -3, -1).

The closest I've gotten is this:

df$y <- ifelse(grepl(df[df$x] < 0), df$y*-1, df$y)

Thanks!

Aucun commentaire:

Enregistrer un commentaire