I want to use values from a different column to replace NA values. In this example my last four values in my df have NAs in the "count" column - I want the NA's to be replaced with values from the "value" column.
Here is a dummy data set
df <- structure(list(First = structure(c(17995, 17997, 17929, 17919,
17808, 18031, NA, NA, NA, NA), class = "Date"), Last =
structure(c(17999,
17998, 17929, 17919, 17809, 18031, 17965, 17965, 17965, 17965
), class = "Date"), days = c(5, 2, 1, 1, 2, 1, NA, NA, NA, NA
), variable = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 2L,
3L), .Label = c("0-12", "0-25", "0-50", "0-100"), class = "factor"),
value = c(1, 0, 1, 1, 1, 1, 1, 0, 1, 1), count = c(5, 0,
1, 1, 2, 1, NA, NA, NA, NA)), class = "data.frame", row.names = c(NA,
-10L))
some thoughts of how it might be resolved.... but that are not working due to my poor coding skills:
df <- df %>%
mutate(a = if_else(count == NA, value, if_else(count >=0, count, a)))
or perhaps there is a way with case_when?
Thanks.
Aucun commentaire:
Enregistrer un commentaire