Re: Subsetting tag data from specific date and time in R Question

155 views
Skip to first unread message

john brzustowski

unread,
Jun 21, 2016, 11:10:04 PM6/21/16
to Howell, Jessica, Motus Wildlife Tracking System
Hi Jessica,

I'm taking the liberty of reposting this question and answering it on the motus
google group.

On Tue, Jun 21, 2016, at 18:05, Howell, Jessica wrote:
> ...
> I was reading the Taylor et al., 2011 paper and am trying to
> determine flight direction as stated in that paper. I am attempting
> to isolate a flight I believe is migratory departure and plot signal
> strength vs. time. I was able to subset the tagged bird within the
> Chaplin East tower data, but am unable to subset the time (between
> 19:14 and 19:16 on July 6th when it flew by). The code for
> subsetting multiple dates was also not working. Would you be able
> to help me with this? Please see code below:
>
> chape = readRDS("2016_USask_ChaplinE_alltags.rds") #load file and remove noise
> chape = subset(chape, freqsd < 0.1 & runLen > 2)
> chape343<-subset(chape, id == "343") #subsetting tag of interest
> chape343d = subset(chape343, ts >= ymd("2016-06-05") & ts < ymd("2016-06-07")) #Message-
> ??????????????Warning messages:
> 1: In eval(expr, envir, enclos) :
> Incompatible methods ("Ops.POSIXt", "Ops.Date") for ">="
> 2: In eval(expr, envir, enclos) :
> Incompatible methods ("Ops.POSIXt", "Ops.Date") for "<"

Up to this point, your code worked fine for me when I tried it with R version 3.1.2, and
I didn't get any warning messages.

> chape343d = subset(chape343, ts = ymd("2016-06-06")) #did not work #how can I isolate a single date?

Two problems:

a) ymd("2016-06-06") represents the instant of midnight, June 5-6,
2016, rather than representing the entire day. "ts" also represents an
instant in time, so that comparison would only pull out detections at
exactly midnight.

b) you need to use '==', not '=' for comparison in the 2nd parameter to subset()

One way to get what you want is:

chape343d = subset(chape343, month(ts) == 6 & day(ts) == 6)

which works by pulling out the month and the day of month from the ts
field, and comparing them each to appropriate values (6 for June, and
6 for the 6th).

>
> chape343d = subset(chape343, ts >= "2016-06-06 19:14:31" & ts < "2015-06-06 19:16:45") #did not work #need to be this specific for making plot
>

In this line, "ts" is a date/time variable, while the quoted
date/times are string variables. Although R often automatically
converts between variable types for comparisons (which is why
id=="343" works in your code above, even though "id" is a numeric
variable and "343" is a string variable), it doesn't do so here.

It's probably best to convert those string representations to actual
date/times using ymd_hms, like so:

chape343d = subset(chape343, ts >= ymd_hms("2016-06-06 19:14:31") & ts < ymd_hms( "2015-06-06 19:16:45") )

J.
Reply all
Reply to author
Forward
0 new messages