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

jim holtman jholtman at gmail.com
Thu Jun 26 01:19:28 CEST 2008


Is this what you want:

> x <- read.table(textConnection("Check1     Check2     HatchDate
+ 101           121           110
+ 130  150 140
+ 140  150  160"), header=TRUE)
> closeAllConnections()
> x
  Check1 Check2 HatchDate
1    101    121       110
2    130    150       140
3    140    150       160
> do.call(rbind, apply(x,1,function(.row){
+     if (.row[3] < .row[2]){
+         # split out the data
+         data.frame(Check1=c(.row[1], .row[3]),
+                    Check2=c(.row[3] - 1, .row[2]),
+                    Check3=c(.row[3], .row[3]),
+                    Stage=c("I", "B"))
+     }
+     else {
+         # normal; just copy over
+         data.frame(Check1=.row[1], Check2=.row[2], Check3=.row[3], Stage="X")
+     }
+ }))
           Check1 Check2 Check3 Stage
Check1        101    109    110     I
HatchDate     110    121    110     B
Check11       130    139    140     I
HatchDate1    140    150    140     B
Check12       140    150    160     X


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