Вот несколько альтернатив.
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
Данные.
Localisation <- scan(what = character(), text = '
A
A
B
A
B
B')
df <- data.frame(Localisation)