can use mutate with a DATE

I have this database:

structure(list(id_mujer = c(8528, 8528, 11711, 11711, 11818,

11818), hpv_post = structure(c(1339459200, 1458172800, 1443571200,

1443571200, 1354838400, 1525392000), tzone = "UTC", class = c("POSIXct",

"POSIXt")), hpv_post_res = c("NEG", "NEG", "POS", "POS", "NEG",

"NEG")), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"

))
# A tibble: 6 x 3

id_mujer hpv_post hpv_post_res

<dbl> <dttm> <chr>

1 8528 2012-06-12 00:00:00 NEG

2 8528 2016-03-17 00:00:00 NEG

3 11711 2015-09-30 00:00:00 POS

4 11711 2015-09-30 00:00:00 POS

5 11818 2012-12-07 00:00:00 NEG

6 11818 2018-05-04 00:00:00 NEG

I need to create a column called "positive" where the hpv_post_res of an observation is "POS" put the hpv_post of that observation, and if not a 0. The problem is that when I do this:

base_contactos_3 %>%

mutate(positivo= if_else(hpv_post_res=="POS", hpv_post, 0))

For some reason I get this error:

Error: Problem with mutate() column positivo.


i `positivo = if_else(hpv_post_res == "POS", hpv_post, 0)`.

x 'origin' must be supplied

Run `rlang::last_error()` to see where the error occurred.

when a say that if the condition is false put a 0 is only because i dont know what to do and it is not transcendent. But if instead of a 0 i put a string like:

base_contactos_3 %>%

mutate(positivo= if_else(hpv_post_res=="POS", hpv_post, "negative"))

It gives me this error:

Error: Problem with `mutate()` column `positivo`.

i `positivo = if_else(hpv_post_res == "POS", hpv_post, "negative")`.

x `false` must be a `POSIXct/POSIXt` object, not a character vector.

Run `rlang::last_error()` to see where the error occurred.

And if a put a NA instead of a 0 (which is the option i prefer) it gives me this error:

Error: Problem with `mutate()` column `positivo`.

i `positivo = if_else(hpv_post_res == "POS", hpv_post, NA)`.

x `false` must be a `POSIXct/POSIXt` object, not a logical vector.

Run `rlang::last_error()` to see where the error occurred.

Is this your preferred solution?

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
base_contactos_3 <- structure(list(id_mujer = c(8528, 8528, 11711, 11711, 11818, 11818), 
                                  hpv_post = structure(c(1339459200, 1458172800, 1443571200, 1443571200, 1354838400, 1525392000), tzone = "UTC", class = c("POSIXct","POSIXt")), 
                                  hpv_post_res = c("NEG", "NEG", "POS", "POS", "NEG","NEG")), 
                             row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))

base_contactos_3 %>%
  mutate(positivo= if_else(hpv_post_res=="POS", hpv_post, NA_real_))
#> # A tibble: 6 x 4
#>   id_mujer hpv_post            hpv_post_res positivo           
#>      <dbl> <dttm>              <chr>        <dttm>             
#> 1     8528 2012-06-12 00:00:00 NEG          NA                 
#> 2     8528 2016-03-17 00:00:00 NEG          NA                 
#> 3    11711 2015-09-30 00:00:00 POS          2015-09-30 00:00:00
#> 4    11711 2015-09-30 00:00:00 POS          2015-09-30 00:00:00
#> 5    11818 2012-12-07 00:00:00 NEG          NA                 
#> 6    11818 2018-05-04 00:00:00 NEG          NA

Created on 2022-03-25 by the reprex package (v2.0.1)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.