Hello, For insect mortality data I'm trying to get an R script that will take the data from the raw form and convert it to Lx (% survival) for a number of treatments. The raw data has the number of days lived for each individual for the respective treatment. Thus, for example, when R selects the data for a single treatment I end up with the following vectors:>day=seq(from=0,to=6) >deaths=c(0,0,2,0,0,1,6)where deaths is the number of deaths on a given day. Now I need to create a new vector with the number alive for each day and this is where I'm stuck... I've tried to work various for and while loops but haven't had success. The vector should be:>Alive=c(9,9,7,7,7,6,0)I realize it is a very basic problem that is easily accomplished in one's head or on a spreadsheet but in the context of the size of the data set I wish to have R do it for me. I would welcome any suggestions please. Best regards.
Try ths: Alive <- sum(deaths) - cumsum(deaths) On Tue, Sep 1, 2009 at 3:48 PM, Frostygoat <frostygoat@gmail.com> wrote:> > Hello, > > For insect mortality data I'm trying to get an R script that will take > the data from the raw form and convert it to Lx (% survival) for a > number of treatments. The raw data has the number of days lived for > each individual for the respective treatment. Thus, for example, when > R selects the data for a single treatment I end up with the following > vectors: > > >day=seq(from=0,to=6) > >deaths=c(0,0,2,0,0,1,6) > > where deaths is the number of deaths on a given day. Now I need to > create a new vector with the number alive for each day and this is > where I'm stuck... I've tried to work various for and while loops but > haven't had success. The vector should be: > > >Alive=c(9,9,7,7,7,6,0) > > I realize it is a very basic problem that is easily accomplished in > one's head or on a spreadsheet but in the context of the size of the > data set I wish to have R do it for me. I would welcome any > suggestions please. > > Best regards. > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Assuming that at the end all of them are dead, you can do the following: sum(deaths)-cumsum(deaths) Regards, Moshe. --- On Wed, 2/9/09, Frostygoat <frostygoat at gmail.com> wrote:> From: Frostygoat <frostygoat at gmail.com> > Subject: [R] Basic population dynamics > To: r-help at r-project.org > Received: Wednesday, 2 September, 2009, 4:48 AM > > Hello, > > For insect mortality data I'm trying to get an R script > that will take > the data from the raw form and convert it to Lx (% > survival) for a > number of treatments.? The raw data has the number of > days lived for > each individual for the respective treatment.? Thus, > for example, when > R selects the data for a single treatment I end up with the > following > vectors: > > >day=seq(from=0,to=6) > >deaths=c(0,0,2,0,0,1,6) > > where deaths is the number of deaths on a given day. Now I > need to > create a new vector with the number alive for each day and > this is > where I'm stuck... I've tried to work various for and while > loops but > haven't had success.? The vector should be: > > >Alive=c(9,9,7,7,7,6,0) > > I realize it is a very basic problem that is easily > accomplished in > one's head or on a spreadsheet but in the context of the > size of the > data set I wish to have R do it for me. I would welcome > any > suggestions please. > > Best regards. > > ______________________________________________ > 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. >