I'd use [
to subset and [<-
to replace. You can do this with entirely vectorised operations (even though it looks a little untidy). Without using data.table
I would reckon this would be the fastest way in base
R...
rows <- df$V2 != 0
df[ rows , "V1" ] <- df[ rows , "V1" ] + 10 - df[ rows , "V2" ]
# V1 V2
#1 970068340 0
#2 970068350 0
#3 970068370 6
#4 970068380 6
#5 970068380 0
#6 970068400 4