Find and replace in R for categorical variables [d

2019-09-27 17:26发布

问题:

This question already has an answer here:

  • Replace values in a vector based on another vector 2 answers
x1 <- c("agree","disagree","agree","agree","agree","disagree","disagree")

How to replace agree=1 and disagree=0 for large atomic vectors or specific row/columns in a data frame?

回答1:

You can do something like this

ifelse(x1 == 'agree', 1, 0)

More conditions can be added as necessary.



回答2:

We can use + to convert the logical to binary

+(x1=="agree")

Or using as.integer (as per comments)

as.integer(x1 == "agree")

This method is much faster than the ifelse.

 set.seed(24)
 x2 <- sample(c("agree", "disagree"), 1e7, replace=TRUE)
 system.time(+(x2=="agree"))
 #  user  system elapsed 
 #  0.32    0.06    0.40 
 system.time(ifelse(x2=="agree", 1,0))
 #  user  system elapsed 
 #   3.20    0.91    4.10 


标签: r string atomic