[R] adding a date column with dplyr

Alice Domalik adomalik at sfu.ca
Sat Aug 6 02:59:45 CEST 2016


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 


-- 
Don MacQueen 

Lawrence Livermore National Laboratory 
7000 East Ave., L-627 
Livermore, CA 94550 
925-423-1062 





On 8/5/16, 9:51 AM, "R-help on behalf of Alice Domalik" 
<r-help-bounces at r-project.org on behalf of 9add2 at queensu.ca> wrote: 

>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 
> 
> 
> 
> 
> [[alternative HTML version deleted]] 
> 
>______________________________________________ 
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 
>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. 



	[[alternative HTML version deleted]]



More information about the R-help mailing list