Manon Billard
Manon Billard

Reputation: 85

How transform a factor to numeric binary variable?

I have a column with different types of sites (factor) :

Localisation
     A  
     A  
     B  
     A 
     B 
     B

I would like to create a new column with binary values (numeric) who correspond to Localization column : A = 1 and B = 0

Localisation Binom 
     A         1
     A         1
     B         0
     A         1
     B         0
     B         0

Thanks !

Upvotes: 4

Views: 454

Answers (3)

akrun
akrun

Reputation: 887078

Another option is

+(df$Localisation != "B")

Upvotes: 1

Rui Barradas
Rui Barradas

Reputation: 76402

Here are several alternatives.

library(dplyr)
library(microbenchmark)

mb <- microbenchmark(
  NelsonGon = ifelse(df$Localisation %in% "A",1,0),
  Edward = ifelse(df$Localisation == "A",1,0),
  Edward2 = +(df$Localisation == "A"),
  Rui = 2L - as.integer(df$Localisation),
  massisenergy = df %>% mutate(Binom = case_when(Localisation == "A" ~ 1, #condition1
                                                 Localisation == "B" ~ 0) #condition2
  )
)

print(mb, unit = "relative", order = "median")
#Unit: relative
#         expr        min         lq       mean     median        uq        max neval cld
#          Rui   1.000000   1.000000   1.000000   1.000000  1.000000  1.0000000   100 a  
#    NelsonGon   4.107345   3.041659   2.490878   2.679642  2.341985  0.4714148   100 ab 
#      Edward2   4.358608   3.339862   2.834451   3.032853  2.741840  1.0814972   100 ab 
#       Edward   7.631876   5.320371   4.330419   4.575165  3.967027  1.4852678   100  b 
# massisenergy 247.792745 161.000287 117.762537 131.729545 96.032138 22.5566734   100   c

Data.

Localisation <- scan(what = character(), text = '
A  
A  
B  
A 
B 
B')
df <- data.frame(Localisation)

Upvotes: 3

massisenergy
massisenergy

Reputation: 1820

approach, handy when there are more than two if-else conditions.

df <- read.table(stringsAsFactors = T, header = T, text = "Localisation
+      A  
+      A  
+      B  
+      A 
+      B 
+      B")

df %>% mutate(Binom = case_when(Localisation == "A" ~ 1, #condition1
                                Localisation == "B" ~ 0) #condition2
             )

Upvotes: 4

Related Questions