sockevalley
sockevalley

Reputation: 361

Replace values in R, "Yes" to 1 and "No" to 0

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.

Upvotes: 12

Views: 79818

Answers (3)

BENY
BENY

Reputation: 323226

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

or simply use:

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

Upvotes: 6

Nico Coallier
Nico Coallier

Reputation: 686

You can easily do this with dplyr.

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

Hope this helps

Upvotes: 17

sockevalley
sockevalley

Reputation: 361

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!

Upvotes: 4

Related Questions