I am trying to find the lowest date available in my data set across several dates per ID - but the lowest date needs to be higher than 'rec_progdato'.
The date format is POSIXct
.
head(df)
start_1_op start_1_strål start_2_strål start_3_strål start_4_strål start_1_chemo
<dttm> <dttm> <dttm> <lgl> <lgl> <dttm>
1 NA NA NA NA NA NA
2 2018-06-28 00:00:00 NA NA NA NA 2018-07-27 00:00:00
3 NA NA NA NA NA NA
4 NA 2019-10-16 00:00:00 NA NA NA NA
5 2018-09-05 00:00:00 NA NA NA NA NA
6 NA NA NA NA NA NA
# ... with 66 more variables: start_2_chemo <dttm>, start_3_chemo <lgl>, start_4_chemo <lgl>, start_1_et <dttm>,
# start_3_her <dttm>, start_4_her <dttm>, start_1_anden <dttm>, start_1_ingen <dttm>, start_of_line <dttm>
# slut_1_chemo <dttm>, slut_2_chemo <dttm>, slut_3_chemo <lgl>, slut_4_chemo <lgl>, slut_1_et <dttm>,
# slut_2_et <dttm>, slut_3_et <dttm>, slut_4_et <lgl>, slut_1_her <dttm>, slut_2_her <dttm>, slut_3_her <dttm>,
# slut_4_her <dttm>, slut_1_anden <dttm>, slut_1_ingen <dttm>, slut_fakedato <lgl>, cut_off <dttm>,
# slut_1_strål <dttm>, slut_2_strål <dttm>, slut_3_strål <lgl>, slut_4_strål <lgl>, slut_1_op <dttm>,
I have used the following to find the lowest date
df <- df %>%
rowwise() %>%
mutate(start_of_line = min(as.Date(c_across(starts_with('start'))),
na.rm = TRUE))
My problem now is, that I can't get anything to work inorder to make sure that the minimum date chosen is GREATER THAN rec_progdato.
Any help is highly appreciated
structure(list(start_1_op = structure(c(NA, 1530144000, NA, NA,
1536105600, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA), tzone = "UTC", class = c("POSIXct", "POSIXt")), start_1_strål = structure(c(NA,
NA, NA, 1571184000, NA, NA, NA, NA, NA, NA, 1470096000, NA, NA,
NA, NA, NA, NA, NA, NA, NA), tzone = "UTC", class = c("POSIXct",
"POSIXt")), start_2_strål = structure(c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_), tzone = "UTC", class = c("POSIXct",
"POSIXt")), start_3_strål = c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), start_4_strål = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA), start_1_chemo = structure(c(NA, 1532649600, NA,
NA, NA, NA, NA, 1572220800, NA, NA, NA, NA, 1422921600, NA, NA,
NA, NA, NA, 1600732800, NA), tzone = "UTC", class = c("POSIXct",
"POSIXt")), start_2_chemo = structure(c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_), tzone = "UTC", class = c("POSIXct",
"POSIXt")), start_3_chemo = c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), start_4_chemo = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA), start_1_et = structure(c(NA, 1554768000, 1398211200,
NA, 1521072000, NA, NA, NA, NA, 1591056000, 1477958400, NA, NA,
1515456000, NA, NA, NA, NA, NA, 1593129600), tzone = "UTC", class = c("POSIXct",
"POSIXt")), start_2_et = structure(c(NA, NA, NA, NA, NA, NA,
NA, NA, NA, 1607385600, NA, NA, NA, 1524700800, NA, NA, 1435622400,
NA, NA, NA), tzone = "UTC", class = c("POSIXct", "POSIXt")),
start_3_et = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), tzone = "UTC", class = c("POSIXct",
"POSIXt")), start_4_et = c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), start_1_her = structure(c(1424908800,
1524182400, 1415145600, 1573689600, 1521072000, 1441670400,
1583280000, 1572220800, 1566432000, 1552953600, NA, 1520380800,
1422921600, NA, 1480896000, 1487635200, 1450310400, 1476403200,
1600732800, 1581897600), tzone = "UTC", class = c("POSIXct",
"POSIXt")), start_2_her = structure(c(NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), tzone = "UTC", class = c("POSIXct", "POSIXt")), start_3_her = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), tzone = "UTC", class = c("POSIXct", "POSIXt")),
start_4_her = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), tzone = "UTC", class = c("POSIXct",
"POSIXt")), start_1_anden = structure(c(NA, NA, 1416787200,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
1437955200, NA), tzone = "UTC", class = c("POSIXct", "POSIXt"
)), start_1_ingen = structure(c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), tzone = "UTC", class = c("POSIXct",
"POSIXt")), start_of_line = structure(c(1424908800, 1524182400,
1398211200, 1571184000, 1521072000, 1441670400, 1583280000,
1572220800, 1566432000, 1552953600, 1470096000, 1520380800,
1422921600, 1515456000, 1480896000, 1487635200, 1435622400,
1476403200, 1437955200, 1581897600), tzone = "UTC", class = c("POSIXct",
"POSIXt")), slut_1_chemo = structure(c(NA, 1553558400, NA,
NA, NA, NA, NA, 1583193600, NA, NA, NA, NA, 1437004800, NA,
NA, NA, NA, NA, 1606521600, NA), tzone = "UTC", class = c("POSIXct",
"POSIXt")), slut_2_chemo = structure(c(NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), tzone = "UTC", class = c("POSIXct", "POSIXt")), slut_3_chemo = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA), slut_4_chemo = c(NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
slut_1_et = structure(c(NA, 1585785600, 1415145600, NA, 1575590400,
NA, NA, NA, NA, 1607385600, 1481587200, NA, NA, 1524700800,
NA, NA, NA, NA, NA, NA), tzone = "UTC", class = c("POSIXct",
"POSIXt")), slut_2_et = structure(c(NA, NA, NA, NA, NA, NA,
NA, NA, NA, 1625788800, NA, NA, NA, 1529452800, NA, NA, 1491350400,
NA, NA, NA), tzone = "UTC", class = c("POSIXct", "POSIXt"
)), slut_3_et = structure(c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), tzone = "UTC", class = c("POSIXct",
"POSIXt")), slut_4_et = c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), slut_1_her = structure(c(1437523200,
1585785600, NA, 1588032000, 1575590400, 1443484800, NA, 1583193600,
1586908800, 1625788800, NA, 1537747200, 1437004800, NA, 1486339200,
1499040000, NA, 1486944000, 1606521600, NA), tzone = "UTC", class = c("POSIXct",
"POSIXt")), slut_2_her = structure(c(NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), tzone = "UTC", class = c("POSIXct", "POSIXt")), slut_3_her = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), tzone = "UTC", class = c("POSIXct", "POSIXt")),
slut_4_her = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), tzone = "UTC", class = c("POSIXct",
"POSIXt")), slut_1_anden = structure(c(NA, NA, 1416787200,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA), tzone = "UTC", class = c("POSIXct", "POSIXt")),
slut_1_ingen = structure(c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), tzone = "UTC", class = c("POSIXct",
"POSIXt")), slut_fakedato = c(NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), cut_off = structure(c(1601510400,
1601510400, 1601510400, 1601510400, 1601510400, 1601510400,
1601510400, 1601510400, 1601510400, 1601510400, 1601510400,
1601510400, 1601510400, 1601510400, 1601510400, 1601510400,
1601510400, 1601510400, 1601510400, 1601510400), tzone = "UTC", class = c("POSIXct",
"POSIXt")), slut_1_strål = structure(c(NA, NA, NA, 1572048000,
NA, NA, NA, NA, NA, NA, 1470960000, NA, NA, NA, NA, NA, NA,
NA, NA, NA), tzone = "UTC", class = c("POSIXct", "POSIXt"
)), slut_2_strål = structure(c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), tzone = "UTC", class = c("POSIXct",
"POSIXt")), slut_3_strål = c(NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), slut_4_strål = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA), slut_1_op = structure(c(NA, 1530230400,
NA, NA, 1536192000, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA), tzone = "UTC", class = c("POSIXct",
"POSIXt")), max_date = structure(c(1437530400, 1585792800,
1416790800, 1588039200, 1575594000, 1443492000, NA, 1583197200,
1586916000, 1625796000, 1481590800, 1537754400, 1437012000,
1529460000, 1486342800, 1499047200, 1491357600, 1486947600,
1606525200, NA), tzone = "UTC", class = c("POSIXct", "POSIXt"
)), slut_progdato = structure(c(1437523200, 1585785600, 1446681600,
NA, 1575590400, 1443484800, NA, 1583193600, 1586908800, 1625788800,
NA, 1537747200, 1437004800, NA, 1486339200, 1499040000, 1491350400,
NA, 1606521600, NA), tzone = "UTC", class = c("POSIXct",
"POSIXt")), slut_doedsdato = structure(c(1463875200, NA,
1488412800, 1598745600, NA, 1446163200, NA, NA, NA, NA, 1484611200,
1543795200, NA, 1529452800, 1522454400, 1579651200, 1536278400,
1486944000, NA, NA), tzone = "UTC", class = c("POSIXct",
"POSIXt")), op_uafsluttet = c(NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), strål1_uafsluttet = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA), strål2_uafsluttet = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
), strål3_uafsluttet = c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), strål4_uafsluttet = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA), chemo1_uafsluttet = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_), chemo2_uafsluttet = c(NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_), chemo3_uafsluttet = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA), chemo4_uafsluttet = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
), et1_uafsluttet = c(NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "1"), et2_uafsluttet = c(NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_), et3_uafsluttet = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA), et4_uafsluttet = c(NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
her1_uafsluttet = c(NA, NA, "1", NA, NA, NA, "1", NA, NA,
NA, NA, NA, NA, NA, NA, NA, "1", NA, NA, "1"), her2_uafsluttet = c(NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_), her3_uafsluttet = c(NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_), her4_uafsluttet = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA), anden1_uafsluttet = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "1",
NA), ingen1_uafsluttet = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_), uafsluttet = c("0", "0", "1",
"0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "1", "0", "1", "1"), end_of_line = structure(c(1437523200,
1585785600, 1446681600, 1588032000, 1575590400, 1443484800,
1601510400, 1583193600, 1586908800, 1625788800, 1481587200,
1537747200, 1437004800, 1529452800, 1486339200, 1499040000,
1491350400, 1486944000, 1606521600, 1601510400), tzone = "UTC", class = c("POSIXct",
"POSIXt")), tot = c(146, 713, 561, 195, 631, 21, 211, 127,
237, 843, 133, 201, 163, 162, 63, 132, 645, 122, 1951, 227
), end_of_line_NY = structure(c(1437523200, 1585785600, 1446681600,
1588032000, 1575590400, 1443484800, 1601510400, 1583193600,
1586908800, 1601510400, 1481587200, 1537747200, 1437004800,
1529452800, 1486339200, 1499040000, 1491350400, 1486944000,
1601510400, 1601510400), tzone = "UTC", class = c("POSIXct",
"POSIXt")), TOT_ny = c(146, 713, 561, 195, 631, 21, 211,
127, 237, 562, 133, 201, 163, 162, 63, 132, 645, 122, 1893,
227), tot2 = c("1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"), TOT_2 = c("146",
"713", "561", "195", "631", "21", "211", "127", "237", "562",
"133", "201", "163", "162", "63", "132", "645", "122", "1893",
"227"), rec_progdato = structure(c(1424736000, 1530057600,
1411603200, 1570147200, 1521072000, 1441670400, 1583280000,
1572220800, 1534896000, 1591056000, 1470009600, 1520121600,
1422489600, 1515456000, 1480377600, 1487635200, 1450224000,
1475798400, 1598313600, 1593129600), tzone = "UTC", class = c("POSIXct",
"POSIXt")), problem_startdt = c("0", "1", "1", "0", "0",
"0", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "1",
"0", "1", "1")), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
CodePudding user response:
Replace values that are lower than rec_progdato
with NA, and take the min
.
dataStart <- as.data.frame(data[, startsWith(colnames(data), "start")])
dataStart[apply(dataStart, 2, \(x) x < data$rec_progdato)] <- NA
data$start_of_line <- apply(dataStart, 1, FUN = min, na.rm = TRUE)
# [1] "2015-02-26" "2018-04-20" "2014-04-23" "2019-10-16" "2018-03-15" "2015-09-08" "2020-03-04"
# [8] "2019-10-28" "2019-08-22" "2019-03-19" "2016-08-02" "2018-03-07" "2015-02-03" "2018-01-09"
# [15] "2016-12-05" "2017-02-21" "2015-06-30" "2016-10-14" "2015-07-27" NA