Slightly different; just update the values ββyou want to change, not all the values, and select them using ifelse() . For instance.
value<-c(2, 4, 5, 8, 2, 3, 1) tf<-c(TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE) df<-data.frame(value, tf) df <- transform(df, newVals = value)
Provision
> df value tf newVals 1 2 TRUE 1.0 2 4 FALSE 4.0 3 5 FALSE 5.0 4 8 FALSE 8.0 5 2 TRUE 1.0 6 3 FALSE 3.0 7 1 TRUE 0.5
You can break this down a bit if you don't like all the indexing - just create an ind that contains the row indices, where tf is TRUE :
df <- transform(df, newVals = value) ind <- with(df, which(tf)) df[ind, "newVals"] <- df[ind, "value"] / 2 df > df value tf newVals 1 2 TRUE 1.0 2 4 FALSE 4.0 3 5 FALSE 5.0 4 8 FALSE 8.0 5 2 TRUE 1.0 6 3 FALSE 3.0 7 1 TRUE 0.5
source share