I have to sum the values of a column based on an if statement. Here my code:
a <- c(1,2,3)
b <- c(2,2,3)
f <- c(1,2,3)
df <- data.frame(a,b,f)
df
for (i in 1:nrow(df)){
if (df$a[i] == df$b[i]){
w <- sum(df$f)
}
}
My result is 6 while it should be 5, the sum of f[2]=2 + f[3]=3.
Thank you for help