[R] help with splitting parts of data frame

Andras Farkas motyocska at yahoo.com
Sun Sep 28 15:41:41 CEST 2014


Dear All,

please help with the following if you can:

we have:

simt <-seq(0,147,by=1)
simc <-50*exp(-0.01*simt)
out1.2 <-data.frame(simt,simc)

AUC <-c(0,apply(matrix(simc),2,function(x) (diff(simt)*(x[-1]+x[-length(x)]))/2 ))
df <-cbind(out1.2,AUC)

z <-cumsum(rep(24,max(out1.2$simt/24)))

####
first24 <-sum(unlist(c(subset(df, df[, 'simt'] > 0 & df[, 'simt'] <= z[1], 3))))
second24 <-sum(unlist(c(subset(df, df[, 'simt'] > z[1] & df[, 'simt'] <= z[2], 3))))
third24 <-sum(unlist(c(subset(df, df[, 'simt'] > z[2] & df[, 'simt'] <= z[3], 3))))
fourth24 <-sum(unlist(c(subset(df, df[, 'simt'] > z[3] & df[, 'simt'] <= z[4], 3))))
fifth24 <-sum(unlist(c(subset(df, df[, 'simt'] > z[4] & df[, 'simt'] <= z[5], 3))))
sixth24 <-sum(unlist(c(subset(df, df[, 'simt'] > z[5] & df[, 'simt'] <= z[6], 3))))

last24 <-sum(unlist(c(subset(df, df[, 'simt'] > z[6] , 3))))

my end result is to get this vector:

c(first24,second24,third24,fourth24,fifth24,sixth24,last24)

####

the important aspect is that z can be of different length, depending on simt,  so what I am trying to do is to code the section between the #### tags above to accommodate any length of z that is greater then 0. I thought of  split( df , f = df$id ), where we would need to add a column with name id to df based on the values in z in such a way where for example all rows of id where simt <=z[1] would be 1 (or a o whatever) , and 2 (or b or whatever)  if simt >z[1] and <=z[2], and so on,  then we could split df based on id and could work with respective columns... This is one thought, but having a hard time figuring out how to add the id column as such that may accommodate z of changing lengths... Or any other ideas that are more suitable would be welcome,

thanks,

Andras
	[[alternative HTML version deleted]]



More information about the R-help mailing list