Reputation: 118
There are many columns here, and I need to replace the dates with 1 and NA with 0. I would like a dplyr solution. thank you.
df <- data.frame(
id = c(1,2,3),
diabetes = c("12-12-2007",NA,"2-12-2018"),
lipids = c(NA,NA,"12-12-2015"),
stringsAsFactors = FALSE
)
Upvotes: 0
Views: 60
Reputation: 389155
You can do :
df[-1] <- +(!is.na(df[-1]))
df
# id diabetes lipids
#1 1 1 0
#2 2 0 0
#3 3 1 1
Upvotes: 1
Reputation: 26238
df %>% mutate(across(-id, ~ifelse(is.na(.), 0, 1)))
id diabetes lipids
1 1 1 0
2 2 0 0
3 3 1 1
Upvotes: 2