简体   繁体   English

用 1 替换许多列的日期,用 0 替换 NA

[英]Replace dates of many columns with 1 and NA with 0

There are many columns here, and I need to replace the dates with 1 and NA with 0. I would like a dplyr solution.这里有很多列,我需要将日期替换为 1,NA 替换为 0。我想要一个 dplyr 解决方案。 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

)
df %>% mutate(across(-id, ~ifelse(is.na(.), 0, 1)))

  id diabetes lipids
1  1        1      0
2  2        0      0
3  3        1      1

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM