[R] Reassign values based on multiple conditions

David L Carlson dcarlson at tamu.edu
Fri Mar 15 16:54:46 CET 2013


This might get you started, but more data is needed to test this

# First create the data 
Collars <- data.frame(collar=1:5, date=as.POSIXlt(c("01/01/2013", "02/01/2013",
    "04/01/2013", "04/01/2013", "07/01/2013"), format="%m/%d/%y"), 
    data=letters[c(24:26, 1:2)])
Animals <- data.frame(animal=c(1, 1, 1, 2, 2), collar=c(1, 5, 3, 2, 1),
    start_date=as.POSIXlt(c("01/01/2013", "04/01/2013",
    "07/01/2013", "01/01/2013", "04/01/2013"), format="%m/%d/%y"),
    end_date=as.POSIXlt(c("03/01/2013", "06/01/2013",
    "09/01/2013", "03/01/2013", "06/01/2013"), format="%m/%d/%y"))

Now you want to look at each row in Collars and find the number of the animal that it represents in Animals:

> AC <- rep(NA, nrow(Collars))
> for (i in 1:nrow(Collars)) {
+    AC[i] <- Animals$animal[Animals$collar==Collars$collar[i] & 
+         Animals$start_date<=Collars$date[i] & Animals$end_date>=Collars$date[i]]
+ }
Error in AC[i] <- Animals$animal[Animals$collar == Collars$collar[i] &  : 
  replacement has length zero
> 
> AC
[1]  1  2 NA NA NA

AC is the vector of animal numbers that has the same length as the Collars data.frame. In this case only the first two rows in Collars match anything in Animals so the rest are NA and R prints an error message. 

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of John Kane
> Sent: Friday, March 15, 2013 9:07 AM
> To: Cat Cowie; r-help at r-project.org
> Subject: Re: [R] Reassign values based on multiple conditions
> 
> I don't see how the data in the three column table you present is
> enough to produce the four column test.  Should the first table
> actually show repeated collar usage so that you can use the next
> incidence of the collar as the end date e.g
> 1 01/01/2013
> 1 02/04/2013
> 
> and so on?
> 
> Some actual data might be useful.
>  The easiest way to supply data  is to use the dput() function.
> Example with your file named "testfile":
> dput(testfile)
> Then copy the output and paste into your email.  For large data sets,
> you can just supply a representative sample.  Usually,
> dput(head(testfile, 100)) will be sufficient.
> 
> Sorry I'm not more helpful
> 
> 
> John Kane
> Kingston ON Canada
> 
> 
> > -----Original Message-----
> > From: cat.e.cowie at gmail.com
> > Sent: Fri, 15 Mar 2013 12:46:13 +0800
> > To: r-help at r-project.org
> > Subject: [R] Reassign values based on multiple conditions
> >
> > Hi all,
> >
> > I have a simple data frame of three columns - one of numbers (really
> a
> > categorical variable), one of dates and one of data.
> >
> > Imagine:
> >
> > collar date data
> > 1 01/01/2013 x
> > 2 02/01/2013 y
> > 3 04/01/2013 z
> > 4 04/01/2013 a
> > 5 07/01/2013 b
> >
> >
> > The 'collar' is a GPS collar that's been worn by an animal for a
> certain
> > amount of time, and then may have been worn by a different animal
> after
> > changes when the batteries needed to be changed. When an animal was
> > caught
> > and the collar battery needed to be changed, a whole new collar had
> to be
> > put on, as these animals (wild boar and red deer!) were not that easy
> to
> > catch. In order to follow the movements of each animal I now need to
> > create
> > a new column that assigns the 'data' by animal rather than by collar.
> I
> > have a table of dates, e.g....
> >
> > animal collar   start_date    end_date
> >      1              1  01/01/2013   03/01/2013
> >      1              5  04/01/2013   06/01/2013
> >      1              3  07/01/2013   09/01/2013
> >      2              2  01/01/2013   03/01/2013
> >      2              1  04/01/2013   06/01/2013
> >
> > I have so far been able to make multi-conditional tests:
> >
> > animal1test<- (date>=01/01/13 && date<=03/01/13)
> > animal1test2<- (date>=04/01/13 && date<=06/01/13)
> > animal2test<- (date>=04/01/13 && date<=06/01/13)
> >
> > to use in an 'if else' formula:
> >
> >  if(animal1test){
> >   collar[1]="animal1"
> >   } else if(animal1test2){
> >     collar[5]="animal1"
> >   }else if(animal2test)
> >     collar[1]="animal2"
> >     }else "NA"
> >
> > As I'm sure you can see, this is completely inelegant, and also not
> > working
> > for me! Any ideas on how to a achieve this?
> >
> > Thanks SO much in advance,
> > Cat
> >
> > 	[[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.
> 
> ____________________________________________________________
> GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at
> http://www.inbox.com/smileys
> Works with AIM®, MSN® Messenger, Yahoo!® Messenger, ICQ®, Google Talk™
> and most webmails
> 
> ______________________________________________
> 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