## add all positive numbers in the vector x<-c(2.1, 9.3, -1.2, 2.43, -4.32, -7.3, 5.29) a<-0 for(i in 1:length(x)) { if(x[i]>0) a<-a+x[i] ## add to a if x[i]>0 } ## note: a simpler alternative, a<-sum(x[x>0])