This is pretty much equivalent:

tapply(DF$value[DF$choice==1], DF$time[DF$choice==1], sum) /
        tapply(DF$value, DF$time, sum)

And both will probably fail if the number of groups with choice==1 is different than the number overall.

--
David.

On Oct 31, 2009, at 5:14 PM, Dimitris Rizopoulos wrote:

one approach is the following:

# say 'DF' is your data frame, then
with(DF, {
   ind <- choice == 1
   n <- tapply(value[ind], time[ind], sum)
   d <- tapply(value, time, sum)
   n / d
})


I hope it helps.

Best,
Dimitris


parkbomee wrote:
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.

--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014

______________________________________________
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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

______________________________________________
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.

Reply via email to