df[1] <- df[1] + as.integer(df[[1]]) %% 2
will be much faster due to vectorized operations. By the way: You don't need yes
explicitly in ifelse
, but it doesn't affect speed.
The above command adds one second to all odd values. If you want to do the opposite, i.e., add one second to even values, you just have to insert the logical not operator, !
:
df[1] <- df[1] + !as.integer(df[[1]]) %% 2