5

The data below has columns for an individual ID (with repeat observations), Date and Fate.

         ID       Date  Fate
1  BHS_1149 2017-04-11   MIA
2  BHS_1154       <NA>  <NA>
3  BHS_1155       <NA>  <NA>
4  BHS_1156       <NA>  <NA>
5  BHS_1157       <NA>  Mort
6  BHS_1159 2017-04-11 Alive
7  BHS_1169 2017-04-11 Alive
8  BHS_1259       <NA>  <NA>
9  BHS_1260       <NA>  <NA>
10 BHS_1262 2017-04-11   MIA
11 BHS_1262 2017-07-05 Alive
12 BHS_1262 2017-12-06 Alive
13 BHS_1262 2017-12-06   MIA
14 BHS_1262 2018-01-17  Mort

For each ID I want to make a new column that represents the min Date or max Date when Fate is Alive. I have tryed different combinations if including and excluding the na.rm = T argument in the code below but still get the following warnings.

library(tidyverse)
library(lubridate)

dat %>% 
  group_by(ID) %>%
  mutate(
    #the first or min of Date
    FstSurvey = min(Date),
    LstAlive = max(Date[Fate == "Alive"])) %>%
  as.data.frame()

         ID       Date  Fate  FstSurvey   LstAlive
1  BHS_1149 2017-04-11   MIA 2017-04-11       <NA>
2  BHS_1154       <NA>  <NA>       <NA>       <NA>
3  BHS_1155       <NA>  <NA>       <NA>       <NA>
4  BHS_1156       <NA>  <NA>       <NA>       <NA>
5  BHS_1157       <NA>  Mort       <NA>       <NA>
6  BHS_1159 2017-04-11 Alive 2017-04-11 2017-04-11
7  BHS_1169 2017-04-11 Alive 2017-04-11 2017-04-11
8  BHS_1259       <NA>  <NA>       <NA>       <NA>
9  BHS_1260       <NA>  <NA>       <NA>       <NA>
10 BHS_1262 2017-04-11   MIA 2017-04-11 2017-12-06
11 BHS_1262 2017-07-05 Alive 2017-04-11 2017-12-06
12 BHS_1262 2017-12-06 Alive 2017-04-11 2017-12-06
13 BHS_1262 2017-12-06   MIA 2017-04-11 2017-12-06
14 BHS_1262 2018-01-17  Mort 2017-04-11 2017-12-06

Warning messages:
1: In max.default(numeric(0), na.rm = FALSE) :
  no non-missing arguments to max; returning -Inf
2: In max.default(numeric(0), na.rm = FALSE) :
  no non-missing arguments to max; returning -Inf

The code seems to work as expected, but I have not been able to intrepret or avoid the errors and was not able to find a solution though the max or min help pages. The reproducable code is included below.

dat <- structure(list(ID = c("BHS_1149", "BHS_1154", "BHS_1155", "BHS_1156", 
"BHS_1157", "BHS_1159", "BHS_1169", "BHS_1259", "BHS_1260", "BHS_1262", 
"BHS_1262", "BHS_1262", "BHS_1262", "BHS_1262"), Date = structure(c(1491890400, 
NA, NA, NA, NA, 1491890400, 1491890400, NA, NA, 1491890400, 1499234400, 
1512543600, 1512543600, 1516172400), class = c("POSIXct", "POSIXt"
), tzone = ""), Fate = c("MIA", NA, NA, NA, "Mort", "Alive", 
"Alive", NA, NA, "MIA", "Alive", "Alive", "MIA", "Mort")), row.names = c(NA, 
-14L), .Names = c("ID", "Date", "Fate"), class = "data.frame")
JJJ
  • 1,009
  • 6
  • 19
  • 31
B. Davis
  • 3,391
  • 5
  • 42
  • 78
  • Basically, if there are IDs without any rows with `Fate == "Alive"`, then there is no Date that could be used for `LstAlive` and `` is returned instead (same when an ID has no Date at all for `FstSurvey`). I don't see why you should worry about these warnings though. – erc Aug 17 '18 at 11:42

1 Answers1

2

I also like to write code that don't give me errors. Here is a suggestion on how to make the same calculations without warnings. By using ordered first and last instead of min and max you dont get the weird scenarios where r interpret max(NULL) becomes Inf.

dat %>% 
  group_by(ID) %>%
  mutate(FstSurvey = first(Date, 
                     order_by = Date),
         LstAlive  = last(Date[Fate == "Alive"], 
                     order_by = Date[Fate == "Alive"]))
davsjob
  • 1,882
  • 15
  • 10