Replace values in R, “Yes” to 1 and “No” to 0

2020-03-24 01:23发布

问题:

I am working with weatherAUS dataset that is avaiable in the R libraries. I am trying to replace "Yes" to 1 and "No" to 0 in the RainTomorrow column.

I wrote this but it doesn't seem to work:

weather4$RainTomorrow[weather4$RainTomorrow=="Yes"]<-1 

I just says:

Warning message: In [<-.factor(*tmp*, weather4$RainTomorrow == "Yes", value = c(NA, : invalid factor level, NA generated

What does it mean and what should I do? I reckon I should use as.numeric or as.factor somewhere but I don't know how really.

回答1:

You can easily do this with dplyr.

require(dplyr)
weather4 <- weather4 %>%
      mutate(RainToday = ifelse(RainToday == "No",0,1))

Hope this helps



回答2:

library(data.table)   
weather4[,":="(RainTomorrow=ifelse(RainTomorrow=="no",0,1))]

or simply use:

as.numeric(as.factor(weather4$RainTomorrow))


回答3:

This is a fairly common thing when one is testing different models. For instance, decision trees work well with "Yes" and "No". However some Regression models demands 1 and 0. Particular Logistic Regression.

I solved this by using the plyr library. It was extremely easy and convenient. Here is my solution.

Origin of solution is here.

library(plyr)
weather5$RainToday <- revalue(weather5$RainToday, c("Yes"=1))
weather5$RainToday <- revalue(weather5$RainToday, c("No"=0))
head(weather5$RainToday)
[1] 0 1 1 1 1 0
Levels: 0 1

Peace!



标签: r dataset