Hi, community! Long time no see. I meet another interesting question when practicing R.
As you see, dataframe c5 is here. How could I let value in col4 subtract value in col3. Like in first row, 9-4.2=4.8, then value in c5[1,3] is 4.8. And then 4.8-13.0=-8.2, then c5[1,2]=-8.2. The same -8.2-16.6=-24.8, c5[1,1]=-24.8. Other rows operation is almost same. But I try the diff() in R, hysteresis difference is not the same with this question. Maybe a loop or other function in R?
c1<-c(16.6,1.0,10.1,NA,8.0,17.0,2.4,7.6,5.7,11.6,3.6,2.8,6.3,1.5,2.7,16.7,6.7,5.3,12.5)
c2<-c(13.0,11.2,11.0,15.0,10.0,11.5,NA,7.8,9.2,6.6,1.6,8.2,18.0,18.9,7,9,2.9,16.1,17.8)
c3<-c(4.2,5.6,1.4,3.4,5.0,5.8,5.1,8.2,8.8,9.1,1.9,7.7,9.1,10.6,3.7,9.9,10.2,11.5,5)
c4<-c(9,8,1.4,0.2,5.0,5.8,3,8.2,1.2,9.1,1.9,25,9.1,3.6,3.7,11,10.2,33,5)
c5<-data.frame(c1,c2,c3,c4)
colnames(c5)<-c("col1","col2","col3","col4")