[R] data frame manipulation - splitting monitoring interval and assigning stage

Jessi Brown jessilbrown at gmail.com
Thu Jun 26 19:51:11 CEST 2008


I'd like to thank those who contacted me with ideas on how to solve
this little problem. I learned something from looking through each
snippet of code, even if it wasn't doing quite what I'd hoped it would
do. Mark Leeds deserves special thanks, for helping me debug my
several attempts to "improve" the function.

Here's what I've settled on, for the record:

> DFAmke<-data.frame(Check1=c(113, 148, 117, 122, 120, 154), Check2=c(148, 170, 122, 129, 154, 175),
+ HatchDate=c(148, 148, NA, NA, 153, 153))
>
> DFAmke
  Check1 Check2 HatchDate
1    113    148       148
2    148    170       148
3    117    122        NA
4    122    129        NA
5    120    154       153
6    154    175       153
>
>
> final<-do.call(rbind, lapply(1:nrow(DFAmke), function(.index) {
+                temp <- DFAmke[.index,]
+                # what to do in case of missing values in HatchDate
+                if (is.na(temp$HatchDate)){
+                   temp$Stage<-"I"
+                   temp
+                # checking if entire interval is past hatch date and
in Brood stage
+                } else if ( DFAmke$Check1[.index] >=
DFAmke$HatchDate[.index] ) {
+                    temp$Stage<-"B"
+                    temp
+                # checking if entire interval is before hatch date
and in Incubation
+                } else if ( DFAmke$Check1[.index] <
DFAmke$HatchDate[.index] && DFAmke$Check2[.index] <=
+ DFAmke$HatchDate[.index] ) {
+                    temp$Stage<-"I"
+                    temp
+                # splitting remaining cases into two intervals
+                } else if ( DFAmke$Check1[.index] <
DFAmke$HatchDate[.index] && DFAmke$Check2[.index] >
+ DFAmke$HatchDate[.index] ) {
+                    temp<-rbind(temp,temp)
+                    savecheck2<-temp$Check2[1]
+                    temp$Check2[1]<- temp$HatchDate[1]
+                    temp$Stage[1]<- "I"
+                    temp$Check1[2]<-temp$HatchDate[2]
+                    temp$Check2[2]<- savecheck2
+                    temp$Stage[2]<- "B"
+                    temp
+                }}))
> final
   Check1 Check2 HatchDate Stage
1     113    148       148     I
2     148    170       148     B
3     117    122        NA     I
4     122    129        NA     I
5     120    153       153     I
51    153    154       153     B
6     154    175       153     B


I'm sure there are many other ways to accomplish my goal, but the
above works just fine. Thanks again to everyone!

cheers, Jessi Brown

> On Wed, Jun 25, 2008 at 1:29 PM, Jessi Brown <jessilbrown at gmail.com> wrote:
>> Hello, everyone.
>>
>> I'm hoping to prevent myself from doing a lot of pointing and clicking
>> in Excel. I have a dataframe of bird nest check observations, in which
>> I know the date of the first check, the date of the second check (both
>> currently in Julian date format), the status of the nest at the second
>> check (alive or failed), and the date that the nest hatched (i.e.
>> changed from Incubation stages to Brood-rearing stage). Many nests
>> have more than one record, as there were several nest checks
>> throughout the duration of the nesting attempt.
>>
>> What I want to do is assign a nest Stage variable, either Incubation
>> or Brood-rearing. It's very easy to do so when the second nest check
>> was before the hatch date (incubation), or when the first nest check
>> was after the hatch date (brood-rearing). But I can't figure out a
>> quick way to split the interval when it contained both incubation and
>> brood-rearing activities.
>>
>> I'd like to go from:
>>
>> Check1     Check2     HatchDate
>> 101           121           110
>>
>> to:
>>
>> Check1     Check2     HatchDate    Stage
>> 101           109          110               I
>> 110           121          110               B
>>
>> because even though the nest wasn't actually checked on the day of
>> hatching, we know that it transitioned to the next stage on hatch day.
>>
>> There's other covariates as well as the unique nest ID which need to
>> be carried along too, just like HatchDate.
>>
>> If anyone who is good at dataframe manipulation could suggest some
>> code to perform these actions, I would really appreciate it. Thanks in
>> advance.
>>
>>
>> cheers, Jessi Brown
>> Ecology, Evolution, and Conservation Biology
>> University of Nevada, Reno
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?
>



More information about the R-help mailing list