I have a data set like the following: subject visit x1 1 1 0.5 1 2 1.2 1 3 0.7 2 1 0.4 2 2 0.6 2 3 1.0 ..... where x1 is the interval between the two visits. Now I want to calculate the cumulative intervals since the beinging, for example subject visit x1 cum 1 1 0.5 0.5 1 2 1.2 0.5+1.2 1 3 0.7 0.5+1.2+0.7 2 1 0.4 0.4 2 2 0.6 0.4+0.6 2 3 1.0 0.4+0.6+1.0 ..... is there an easy to generate the last veriable cum? The number of visits for each subject may be different. I also want to choose a subset which correspond to the last observation of each subject. I am also wondering if there is a fast way to do so. [[alternative HTML version deleted]]
Hi,
subject=c(1,1,1,2,2,2)
visit=c(1,2,3,1,2,3)
x1=c(0.5,1.2,0.7,0.4,0.6,0.1)
cum=NULL #initialize
for(i in 1:length(subject)){
cum[i]=sum(x1[subject[i]==subject&visit[i]>=visit])
}
I am sure there is a way with tapply or similar functions that is
computationally more efficient. But if your dataset is not very large, the
above function does the job very well.
Cheers,
Daniel
-------------------------
cuncta stricte discussurus
-------------------------
-----Urspr?ngliche Nachricht-----
Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im
Auftrag von gallon li
Gesendet: Monday, November 24, 2008 2:22 AM
An: r-help
Betreff: [R] count the cumulative for each subject
I have a data set like the following:
subject visit x1
1 1 0.5
1 2 1.2
1 3 0.7
2 1 0.4
2 2 0.6
2 3 1.0
.....
where x1 is the interval between the two visits. Now I want to calculate the
cumulative intervals since the beinging, for example
subject visit x1 cum
1 1 0.5 0.5
1 2 1.2 0.5+1.2
1 3 0.7 0.5+1.2+0.7
2 1 0.4 0.4
2 2 0.6 0.4+0.6
2 3 1.0 0.4+0.6+1.0
.....
is there an easy to generate the last veriable cum? The number of visits for
each subject may be different.
I also want to choose a subset which correspond to the last observation of
each subject. I am also wondering if there is a fast way to do so.
[[alternative HTML version deleted]]
______________________________________________
R-help at 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.
How about:
tapply(dat$x1,dat$subject,function(x) cumsum(x))
which gives you a list for each subject.
this can be converted to a vector:
do.call("c",tapply(dat$x1,dat$subject,function(x) cumsum(x)))
So if your data frame is ordered for your subjects:
cbind(your.data.frame,do.call("c",tapply(dat$x1,dat$subject,function(x)
cumsum(x))))
Good luck
Bart
Daniel Malter wrote:>
> Hi,
>
> subject=c(1,1,1,2,2,2)
> visit=c(1,2,3,1,2,3)
> x1=c(0.5,1.2,0.7,0.4,0.6,0.1)
>
> cum=NULL #initialize
> for(i in 1:length(subject)){
> cum[i]=sum(x1[subject[i]==subject&visit[i]>=visit])
> }
>
> I am sure there is a way with tapply or similar functions that is
> computationally more efficient. But if your dataset is not very large, the
> above function does the job very well.
>
> Cheers,
> Daniel
>
>
> -------------------------
> cuncta stricte discussurus
> -------------------------
>
> -----Urspr?ngliche Nachricht-----
> Von: r-help-bounces at r-project.org [mailto:r-help-bounces at
r-project.org] Im
> Auftrag von gallon li
> Gesendet: Monday, November 24, 2008 2:22 AM
> An: r-help
> Betreff: [R] count the cumulative for each subject
>
> I have a data set like the following:
>
> subject visit x1
> 1 1 0.5
> 1 2 1.2
> 1 3 0.7
> 2 1 0.4
> 2 2 0.6
> 2 3 1.0
> .....
>
> where x1 is the interval between the two visits. Now I want to calculate
> the
> cumulative intervals since the beinging, for example
>
> subject visit x1 cum
> 1 1 0.5 0.5
> 1 2 1.2 0.5+1.2
> 1 3 0.7 0.5+1.2+0.7
> 2 1 0.4 0.4
> 2 2 0.6 0.4+0.6
> 2 3 1.0 0.4+0.6+1.0
> .....
>
> is there an easy to generate the last veriable cum? The number of visits
> for
> each subject may be different.
>
> I also want to choose a subset which correspond to the last observation of
> each subject. I am also wondering if there is a fast way to do so.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>
> ______________________________________________
> R-help at 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.
>
>
--
View this message in context:
http://www.nabble.com/count-the-cumulative-for-each-subject-tp20656012p20657233.html
Sent from the R help mailing list archive at Nabble.com.
Peter, After I made a small modification it worked: with(dat, ave(x1, subject, FUN=cumsum)) But what's the use of with? If I use ave(x1, subject, FUN=cumsum), I get the same result? Bart ----- Original Message ----- From: "Bart Joosen" <Bartjoosen at hotmail.com> To: "Peter Dalgaard" <P.Dalgaard at biostat.ku.dk> Cc: <r-help at r-project.org> Sent: Monday, November 24, 2008 7:29 PM Subject: Re: [R] count the cumulative for each subject> Peter, > > I actually took a look at the ave function, but couldn't manage to get it > right. > But when I try your code, I get "Error in as.vector(x, mode) : invalid > argument 'mode'". > Any ideas? > > Bart > > ----- Original Message ----- > From: "Peter Dalgaard" <P.Dalgaard at biostat.ku.dk> > To: "bartjoosen" <bartjoosen at hotmail.com> > Cc: <r-help at r-project.org> > Sent: Monday, November 24, 2008 3:42 PM > Subject: Re: [R] count the cumulative for each subject > > > bartjoosen wrote: >> How about: >> >> tapply(dat$x1,dat$subject,function(x) cumsum(x)) >> which gives you a list for each subject. >> >> this can be converted to a vector: >> do.call("c",tapply(dat$x1,dat$subject,function(x) cumsum(x))) >> >> So if your data frame is ordered for your subjects: >> >> cbind(your.data.frame,do.call("c",tapply(dat$x1,dat$subject,function(x) >> cumsum(x)))) >> >> > > Sounds like a job for the eternally overlookked ave() function: > > with(dat, ave(x1, subject, cumsum)) > > > -- > O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B > c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K > (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 > ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 > >