Tag sum

[Solved] Conditional summation in r

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]) solved Conditional summation in r

[Solved] in R, How to sum by flowing row in a data frame

We could use shift from data.table library(data.table) m1 <- na.omit(do.call(cbind, shift(df1$col1, 0:4, type=”lead”))) rowSums(m1*(1:5)[col(m1)]/5) #[1] 13.60 12.20 31.24 25.58 30.48 32.58 44.88 Or another option m1 <- embed(df1$col1,5) rowSums(m1*(5:1)[col(m1)]/5) #[1] 13.60 12.20 31.24 25.58 30.48 32.58 44.88 solved in R,…