[R] re ferring to data of previous rows

Gabor Grothendieck ggrothendieck at gmail.com
Thu Oct 22 12:26:47 CEST 2009


Next time if dd is your data frame display dput(dd) so we know exactly
what you have.

>From your output I guessed it was numeric but the behavior of the 0/1
column that you describe is consistent with it being a factor rather
than numeric.  Try

dd$of <- as.numeric(levels(dd$of)[dd$of])

or equivalently

dd$of <- as.numeric(as.character(dd$of))

to make them numeric first.

ave() takes each group of rows of dd$of that have the same dd$Id and
applies FUN separately to it forming a vector like dd$Id but with the
output of each application of FUN replacing the input in that vector.

On Thu, Oct 22, 2009 at 2:36 AM, clion <birte_2 at hotmail.com> wrote:
>
> thanks, it works! well, now it shows 1s instead of 0s and 2s instead of 1s,
> which fine with me...but could you explain me what this function actually
> does?
>
>
> Gabor Grothendieck wrote:
>>
>> Iff dd is your data frame then:
>>
>> dd$prev <- ave(dd$of, dd$Id, FUN = function(x) c(NA, head(x, -1)))
>>
>>
>> On Wed, Oct 21, 2009 at 2:55 AM, clion <birte_2 at hotmail.com> wrote:
>>>
>>> Dear Rlers,
>>> in the following dataset I would like to insert a new column that refers
>>> to
>>> the data of the previous row.
>>> My question is whether the probability of a female (Id) changes if she
>>> had
>>> given birth to a pup in the previous year. So my dataframe consists of
>>> the
>>> column Id, year (2003-2007 for each Id) and offspring (=of; 1-0):
>>> Id   year   of
>>> 1   2003   1
>>> 1   2004   0
>>> 1   2005   1
>>> 1   2006   1
>>> 1   2007   0      with 1= female (Id) gave birth to offspring (same year)
>>> 2   2003   0      and 0 = female didn't pup
>>> 2   2004   1
>>> 2   2005   1
>>> 2   2006   0
>>> 2   2007   1
>>> 3   2003   1
>>> 3   2004   1
>>> 3   2005   1
>>> 3   2006   0
>>> 3   2007   0
>>> Now I want to add the column "offspring in the previous year -
>>> yes/no(1-0)"
>>> the output now should look like this:
>>>
>>> Id  year   of   of_inPreviousYear
>>> 1   2003   1   NA
>>> 1   2004   0   1
>>> 1   2005   1   0
>>> 1   2006   1   1
>>> 1   2007   0   1
>>> 2   2003   0   NA
>>> 2   2004   1   0
>>> 2   2005   1   1
>>> 2   2006   0   1
>>> 2   2007   1   0
>>> 3   2003   1   NA
>>> 3   2004   1   1
>>> 3   2005   1   1
>>> 3   2006   0   1
>>> 3   2007   0   0
>>>
>>> any idea how I could program that?
>>>
>>> thanks a lot,
>>> clion
>>> --
>>> View this message in context:
>>> http://www.nabble.com/referring-to-data-of-previous-rows-tp25987364p25987364.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> 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/referring-to-data-of-previous-rows-tp25987364p26004769.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list