Skip to content

adding a date column with dplyr

3 messages · Alice Domalik, MacQueen, Don

#
Hi all,


I'm having some difficulties adding a conditional element to my code.

I have a time series data set that contains GPS tracking coordinates for a population of animals.


ID                Date                                   Latitude           Longitude
15K12         2014-05-22 04:33:00     50.67675        -129.6553

15K12         2014-05-22 04:35:00     50.45613        -129.4566
15K19         2014-05-24 06:44:00     50.34611        -129.5678    (and so on)

I added a new column to this dataset, called "Night", which is the day of tracking for each animal, but with the time of 21:30.


ID                Date                                  Latitude           Longitude       Night
15K12         2014-05-22 04:33:00     50.67675        -129.6553       2014-05-22 21:30:00

15K12         2014-05-22 04:35:00     50.45613        -129.4566       2014-05-22 21:30:00
15K19         2014-05-24 06:44:00     50.34611        -129.5678       2015-05-24 21:30:00

I used the following code to do this:
library(dplyr)
library(lubridate)
Sys.setenv(TZ="Canada/Pacific")
df<-df%>%
  group_by(ID) %>%
  mutate(Night=as.POSIXct(date(min(Date)) + days(0) + hours(21) + minutes(30), tz="Canada/Pacific"))

However, I need to add a conditional element, because for one animal, "Night" needs to be 1 day later. I tried this code...

df<-df%>%
  group_by(ID) %>%
  mutate(Night=ifelse(test=(id=='M16c'),yes=as.POSIXct(date(min(Date)) + days(1) + hours(21) + minutes(30), tz="Canada/Pacific"), no=as.POSIXct(date(min(Date)) + days(0) + hours(21) + minutes(30), tz="Canada/Pacific")))

The code runs, but instead of having a date, I get a string of numbers like "1403497200" in the column "Night".
Any ideas what the problem could be?

Thanks, Alice
#
What's wrong with this?

df$Night <- as.POSIXct( paste(format(df$Date,'%Y-%m-%d'),'21:30'))
  or
df$Night <- trunc(df$Date,'day') + 21*60*60 + 30*60

I believe both of those satisfy "the day of tracking for each animal, but
with the time of 21:30". But perhaps you meant the day that tracking
started, when tracking lasted more than one day...


And then just add 24 hours ("one day later") to that one special case

df$Night[df$ID=='M16c'] <- df$Night[df$ID=='M16c'] + 24*60*60

-Don
#
Thanks so much, that worked! 

----- Original Message -----

From: "Don MacQueen" <macqueen1 at llnl.gov> 
To: "Alice Domalik" <9add2 at queensu.ca>, r-help at r-project.org 
Sent: Friday, August 5, 2016 2:18:16 PM 
Subject: Re: [R] adding a date column with dplyr 

What's wrong with this? 

df$Night <- as.POSIXct( paste(format(df$Date,'%Y-%m-%d'),'21:30')) 
or 
df$Night <- trunc(df$Date,'day') + 21*60*60 + 30*60 

I believe both of those satisfy "the day of tracking for each animal, but 
with the time of 21:30". But perhaps you meant the day that tracking 
started, when tracking lasted more than one day... 


And then just add 24 hours ("one day later") to that one special case 

df$Night[df$ID=='M16c'] <- df$Night[df$ID=='M16c'] + 24*60*60 

-Don