sum - Conditional summation in r -
i have data frame 63 columns , 1128 rows.
the columns consist of weightsrep, lon lat, v1.....v60, v[i] represents precipitation data.
i want create vector [1:60] contains sum of weightsrep v not equal 0.
for example (imagine these columns):
if weights rep = 0.2,0.3,0.2
and v1 = 10,0,8
then sum of weightreps = 0.2+0.2
df <- data.frame(rep=c(0.2,0.3,0.2), lon=c(35,36,37), lat=c(-90,-91,-92), v1=c(10,0,8), v2=c(3,4,5), v3=c(9,20,4)) v <- as.vector("numeric") for(i in 1:3) v[i] <- sum(df$rep[df[,i+3]!=0])
Comments
Post a Comment