I have this base:
structure(list(id_mujer = c(12468, 13882, 15676, 182603, 182603,
299864, 299864, 301483, 301483), primer_contacto = structure(c(1350345600,
1347580800, 1339372800, 1330041600, 1330041600, 1335484800, 1335484800,
1337644800, 1337644800), tzone = "UTC", class = c("POSIXct",
"POSIXt")), segundo_contacto = structure(c(1351728000, 1376352000,
1352764800, 1337212800, 1337212800, 1344988800, 1344988800, 1345075200,
1345075200), tzone = "UTC", class = c("POSIXct", "POSIXt")),
tercer_contacto = structure(c(1553731200, 1530057600, 1556496000,
1439424000, 1439424000, 1373241600, 1373241600, 1387152000,
1387152000), tzone = "UTC", class = c("POSIXct", "POSIXt"
)), positivo = c(NA, NA, NA, NA, "2015-08-13", "2012-08-15",
NA, "2012-08-16", NA)), row.names = c(NA, -9L), groups = structure(list(
id_mujer = c(12468, 13882, 15676, 182603, 299864, 301483),
.rows = structure(list(1L, 2L, 3L, 4:5, 6:7, 8:9), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
# A tibble: 9 x 5
# Groups: id_mujer [6]
id_mujer primer_contacto segundo_contacto tercer_contacto positivo
<dbl> <dttm> <dttm> <dttm> <chr>
1 12468 2012-10-16 00:00:00 2012-11-01 00:00:00 2019-03-28 00:00:00 NA
2 13882 2012-09-14 00:00:00 2013-08-13 00:00:00 2018-06-27 00:00:00 NA
3 15676 2012-06-11 00:00:00 2012-11-13 00:00:00 2019-04-29 00:00:00 NA
4 182603 2012-02-24 00:00:00 2012-05-17 00:00:00 2015-08-13 00:00:00 NA
5 182603 2012-02-24 00:00:00 2012-05-17 00:00:00 2015-08-13 00:00:00 2015-08-13
6 299864 2012-04-27 00:00:00 2012-08-15 00:00:00 2013-07-08 00:00:00 2012-08-15
7 299864 2012-04-27 00:00:00 2012-08-15 00:00:00 2013-07-08 00:00:00 NA
8 301483 2012-05-22 00:00:00 2012-08-16 00:00:00 2013-12-16 00:00:00 2012-08-16
9 301483 2012-05-22 00:00:00 2012-08-16 00:00:00 2013-12-16 00:00:00 NA
The women's ids that are repeated is because they have a positive comment that says NA and another that is a date. What I want to do is convert the positive column into those id so that the 2 observations have the same date, and the observations that are only there once have the same result (in this case NA)
I do this and it doesn't work:
base %>%
mutate(positivo= if_else(!id_mujer %in% c(182603, 299864, 301483, 15676, 12468, 13882),
positivo,
positivo[!is.na(positivo)]))
It gave this:
Error: Problem with `mutate()` column `positivo`.
i `positivo = if_else(...)`.
x objeto de tipo 'closure' no es subconjunto
i The error occurred in group 1: id_mujer = 12468.
Run `rlang::last_error()` to see where the error occurred.
The output I would like is this:
# A tibble: 6 x 5
# Groups: id_mujer [6]
id_mujer primer_contacto segundo_contacto tercer_contacto positivo
<dbl> <dttm> <dttm> <dttm> <chr>
1 12468 2012-10-16 00:00:00 2012-11-01 00:00:00 2019-03-28 00:00:00 NA
2 13882 2012-09-14 00:00:00 2013-08-13 00:00:00 2018-06-27 00:00:00 NA
3 15676 2012-06-11 00:00:00 2012-11-13 00:00:00 2019-04-29 00:00:00 NA
4 182603 2012-02-24 00:00:00 2012-05-17 00:00:00 2015-08-13 00:00:00 2015-08-13
5 299864 2012-04-27 00:00:00 2012-08-15 00:00:00 2013-07-08 00:00:00 2012-08-15
6 301483 2012-05-22 00:00:00 2012-08-16 00:00:00 2013-12-16 00:00:00 2012-08-16