Hi all, I am trying to figure out a way to improve my code's efficiency by avoiding the use of loop. I want to calculate a conditional mean(?) given time. For example, from the data below, I want to calculate sum((value|choice==1)/sum(value)) across time. Is there a way to do it without using a loop?
time cum_time choice value 1 4 1 3 1 4 0 2 1 4 0 3 1 4 0 3 2 6 1 4 2 6 0 4 2 6 0 2 2 6 0 4 2 6 0 2 2 6 0 2 3 4 1 2 3 4 0 3 3 4 0 5 3 4 0 2 My code looks like objective[1] = value[1] / sum(value[1:cum_time[1]) for (i in 2:max(time)){ objective[i] = value[cum_time[i-1]+1] / sum(value[(cum_time[i-1]+1) : cum_time[i])]) } sum(objective) Anyone have an idea that I can do this without using a loop?? Thanks. _________________________________________________________________ [[elided Hotmail spam]] [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.