Tuesday, 15 March 2011

r - Cumulative sum based on specified values -



r - Cumulative sum based on specified values -

i have dataframe containing 1 column (depth, z) in trying find difference in accumulative depth values based on regular depth values. create new dataframe 2 columns: criteria value , respective cumulative depth value, example:

z<-as.data.frame(c(1:20)) #original depth values. crit<-c(5,10,15,20) #criteria choosing cumulative z-values crit.d<-as.data.frame(crit) #new dataframe respective cumulative values should added sum(subset(z, c(1:20) <= 5)) # give me first cumulative value need.

however values crit =10, 15 , 20. ideally new info frame

crit cumulative 5 15 10 55 15 120 20 210

note: simplified representation of data, , calculating cumulative z value @ every x-th position won't work. more reflective sample of info set be

z1<-c(1.2, 1.5, 0.8, 0.7, 1.6, 1.9, 1.1, 0.6, 1.3, 1.0) z1<-data.frame(z1) crit1<-c(0.5,1,1.5,2) # loop comes mind, for(i in c(0.5,1,1.5,2)){ print( sum(subset(z1,z1<=i))) } # error, because cannot utilize integers error in fun(x[[1l]], ...) : defined on info frame numeric variables

attempting cumsum

cumsum(z1)[seq(0.5,2,by=0.5)] # doesn't work either

merge you.

z <- as.data.frame(c(1:20)) merge(crit.d, data.frame(crit=z[[1]], cumulative=cumsum(z[[1]])) ) crit cumulative 1 5 15 2 10 55 3 15 120 4 20 210

r

No comments:

Post a Comment