jeudi 23 janvier 2020

Sum one hour to my date depending on another column value

I would like to add one hour to my date values whenever I have a condition on another column. Now I have this code, but it doesn't change any time at all

x$date <- as.POSIXct (x$date)
library(lubridate)
ifelse(x$value > 2, x$a <- x$date+ hours(1) , x$a <- x$date)

I've tried to generate two new columns instead of one, but what it does is adding to x$a one hour to all the rows instead of going row by row.

library(lubridate)
ifelse(x$value > 2, x$a <- x$date+ hours(1) , x$b <- x$date)

I imagine it is something to be with how ifelse consider the condition. If there is one row that fulfils this requirement it does it for all the data in the column.

How do I get this new hour per row depending on another column?

Aucun commentaire:

Enregistrer un commentaire