replace NA with 0 and all other values/text as 1

前端 未结 3 1197
误落风尘
误落风尘 2021-01-29 00:58
airquality

    Ozone Solar.R Wind Temp Month Day
1      41     190  7.4   67     5   1
2      36     118  8.0   72     5   2
3      12     149 12.6   74     5   3
4             


        
相关标签:
3条回答
  • 2021-01-29 01:25

    Assuming your dataframe is called airquality

    airquality$Ozone <- ifelse(is.na(airquality$Ozone), 0, 1) 
    
    0 讨论(0)
  • 2021-01-29 01:33
    airquality$Ozone <- as.integer(!is.na(airquality$Ozone))
    
    0 讨论(0)
  • 2021-01-29 01:36

    Alternatively

    airquality$Ozone[!is.na(airquality$Ozone)] <- 1L
    airquality$Ozone[is.na(airquality$Ozone)] <- 0L
    
    0 讨论(0)
提交回复
热议问题