0

First off, I've found 2 other similar questions on stack overflow, but I still can't figure out how to solve this particular question...

Intially, I wanted to create a new column in my data frame that contained the the date each bird was tracked, but with the time of 21:30.

For example, I wanted to go from this...

BirdID    latitude      longitude   Date
15K12     492719.9      5634805     2015-06-23 18:25:00
15K12     492491.5      5635018     2015-06-23 18:27:00
15K70     455979.1      5653581     2015-06-24 19:54:00  
15K70     456040.9      5653668     2015-06-24 19:59:00

To this....

BirdID   latitude    longitude   Date                    Night
15K12   492719.9     5634805     2015-06-23 04:25:00     2015-06-23 21:30:00
15K12   492491.5     5635018     2015-06-23 04:27:00     2015-06-23 21:30:00
15K70   455979.1     6535815     2015-06-24 03:54:00     2015-06-24 21:30:00 
15K70   456040.9     5653668     2015-06-24 03:59:00     2015-06-24 21:30:00

To accomplish this, I used this code....

df <- df %>%
  group_by(BirdID) %>%
  mutate(night=as.POSIXct(date(min(Date)) + days(0) + hours(21) +
  minutes(30),tz="Canada/Pacific"))

However, now I want use slightly different times for some of the individuals. eg, for BirdID= 15K12, 15K70, 15K30 I would like the time to be 21:30 but for BirdID= 14K22, 14K50, 14K62 I would like the time to be 21:20

How do I incorporate this condition into my code?

Sumedh
  • 4,835
  • 2
  • 17
  • 32
Splash1199
  • 379
  • 3
  • 14
  • Have you checked `ifelse`, have a look at this [link](https://stat.ethz.ch/R-manual/R-devel/library/base/html/ifelse.html). – thepule Aug 05 '16 at 07:58
  • I tried this line but it didn't work `df<-df%>% group_by(BirdID) %>% mutate(Night= ifelse(test=(BirdID=='99b'|'99'|'C17'|'C1'|'M16c'|'M17a'),yes=as.POSIXct(date(min(Date)) + days(0) + hours(21) + minutes(20),tz="Canada/Pacific"),no=ifelse(test=(BirdID=='15K12'|'M7'|'C7'|'M1'), yes=as.POSIXct(date(min(Date)) + days(0) + hours(21) + minutes(50),tz="Canada/Pacific"),no=NA)))` Error: operations are possible only for numeric, logical or complex types – Splash1199 Aug 05 '16 at 15:54
  • This is obviously old, but `case_when` is the ideal solution for multiple conditions. You can easily do something like `df %>% mutate(Night = case_when( BirdID %in% c('99b','99','C17','C1','M16c','M17a') ~ as.POSIXct(date(min(Date)) + days(0) + hours(21) + minutes(20),tz="Canada/Pacific"), ... )))` and it's much more readable. – GenesRus Sep 26 '19 at 19:35

1 Answers1

0

Here is a solution using str_sub from stringr package and paste:

df = data.frame(BirdID = sample(LETTERS[1:6],20, replace = T), 
Date = latemail(20, st = "2015/06/20", et = "2015/06/25"))
# for latemail function see 
http://stackoverflow.com/questions/14720983/efficiently-generate-a-random-sample-of-times-and-dates-between-two-dates 

library(stringr)
group_1 = c("A", "D", "E") # 15K70 in your example
group_2 = c("B", "C", "F") # 14K22 in your example

df$Night = as.POSIXct(paste(str_sub(df$Date,1,10), "21:30:00"))
df$Night[df$BirdID %in% group_2] = 
as.POSIXct(paste(str_sub(df$Date[df$BirdID %in% group_2],1,10), "21:20:00"))

df

   BirdID                Date               Night
1       F 2015-06-20 04:01:12 2015-06-20 21:20:00
2       F 2015-06-21 06:39:40 2015-06-21 21:20:00
3       B 2015-06-21 08:26:48 2015-06-21 21:20:00
4       E 2015-06-21 11:38:00 2015-06-21 21:30:00
5       A 2015-06-21 18:11:13 2015-06-21 21:30:00
6       C 2015-06-22 03:06:05 2015-06-22 21:20:00
7       A 2015-06-22 03:50:52 2015-06-22 21:30:00
8       C 2015-06-22 05:22:32 2015-06-22 21:20:00
9       F 2015-06-22 07:09:57 2015-06-22 21:20:00
10      C 2015-06-22 15:17:08 2015-06-22 21:20:00
11      E 2015-06-23 05:29:51 2015-06-23 21:30:00
12      C 2015-06-23 09:10:33 2015-06-23 21:20:00
13      D 2015-06-23 09:17:44 2015-06-23 21:30:00
14      F 2015-06-23 11:15:53 2015-06-23 21:20:00
15      F 2015-06-24 04:40:47 2015-06-24 21:20:00
16      D 2015-06-24 04:49:52 2015-06-24 21:30:00
17      F 2015-06-24 05:05:39 2015-06-24 21:20:00
18      F 2015-06-24 09:50:55 2015-06-24 21:20:00
19      F 2015-06-24 15:17:53 2015-06-24 21:20:00
20      C 2015-06-24 20:17:21 2015-06-24 21:20:00
bVa
  • 3,839
  • 1
  • 13
  • 22