I have a row of data that is created through ifelse statement of proper data.
mutate(yrsatrisk = ifelse(!is.na(Age_at_death), as.numeric(Age_at_death), ifelse(!is.na(Age_at_last_update), as.numeric(Age_at_last_update), 0))
However one of the datapoints age_at_last_update = 8 but is then recognized as 80. If i sort the output, that row is set at 80, everything else recognizes it as 80 but one with if else statement that does yrsatrisk - 74 which gives an output of -66
1060 Male 79 15 15 15 15 15 5 30 30 20
527 979 Female 79 15 15 15 15 15 5 30 30 20
26 828 Male 8 15 15 15 15 15 -66 30 30 -51
150 101 Female 80 15 15 15 15 15 6 30 30 21
which is created by
"0-14atrisk" = ifelse(yrsatrisk > 14, 15, yrsatrisk),
"15-29atrisk" = ifelse(yrsatrisk > 29, 15, ifelse(yrsatrisk > 14 & yrsatrisk < 30, (as.numeric(yrsatrisk) - 14), 0)),
"30-44atrisk" = ifelse(yrsatrisk > 44, 15, ifelse(yrsatrisk > 29 & yrsatrisk < 45, (as.numeric(yrsatrisk) - 29), 0)),
"45-59atrisk" = ifelse(yrsatrisk > 59, 15, ifelse(yrsatrisk > 44 & yrsatrisk < 60, (as.numeric(yrsatrisk) - 44), 0)),
"60-74atrisk" = ifelse(yrsatrisk > 74, 15, ifelse(yrsatrisk > 59 & yrsatrisk < 75, (as.numeric(yrsatrisk) - 59), 0)),
"75-85+atrisk" = ifelse(yrsatrisk > 74, (as.numeric(yrsatrisk) - 74) , 0),
"0-29atrisk" = ifelse(yrsatrisk > 29, 30, yrsatrisk),
"30-59atrisk" = ifelse(yrsatrisk > 59, 30, ifelse(yrsatrisk > 29 & yrsatrisk < 60, yrsatrisk, 0)),
"60-85+atrisk" = ifelse(yrsatrisk > 59, (as.numeric(yrsatrisk) - 59), 0))
This is not fixed by forcing yrsatrisk with as.numeric prior to the mutate either. It currently means I have to output the data and edit it and re-input for it to work.
Any ideas?
Dput of couple lines around the 8
structure(list(PID = c(696L, 1060L, 979L, 828L, 101L, 1470L),
Gender = c("Female", "Male", "Female", "Male", "Female",
"Male"), yrsatrisk = c("79", "79", "79", "8", "80", "80"),
`0-14atrisk` = c("15", "15", "15", "15", "15", "15"), `15-29atrisk` = c(15,
15, 15, 15, 15, 15), `30-44atrisk` = c(15, 15, 15, 15, 15,
15), `45-59atrisk` = c(15, 15, 15, 15, 15, 15), `60-74atrisk` = c(15,
15, 15, 15, 15, 15), `75-85+atrisk` = c(5, 5, 5, -66, 6,
6), `0-29atrisk` = c("30", "30", "30", "30", "30", "30"),
`30-59atrisk` = c("30", "30", "30", "30", "30", "30"), `60-85+atrisk` = c(20,
20, 20, -51, 21, 21)), row.names = 330:335, class = "data.frame")
The expected output should be
26 828 Male 8 8 0 0 0 0 0 8 0 0