У меня есть 5 условий, которые могут быть present
(= 1) или not
(= 0):
set.seed(101)
df <- data.frame(
alfa = sample(c(0, 1), 30, replace = TRUE),
beta = sample(c(0, 1), 30, replace = TRUE),
gamma = sample(c(0, 1), 30, replace = TRUE),
delta = sample(c(0, 1), 30, replace = TRUE),
epsilon = sample(c(0, 1), 30, replace = TRUE)
)
Я хочу создать набор фиктивных переменных из всех возможных комбинаций(2 5 ) о наличии этих условий.В частности, я хотел бы проверить сопутствующее наличие следующих условий:
- 1)
alfa + beta
; - 2)
alfa + gamma
; - 3)
alfa + delta
; - 4)
alfa + epsilon
; - 5)
alfa + beta + gamma
; - [...]
- 27)
alfa + beta + gamma + delta + epsilon;
Создание матрицы всех возможных комбинаций
v <- as.matrix(expand.grid(rep(list(c(FALSE, TRUE)), ncol(df))))
Все комбинации:
head(v)
Var1 Var2 Var3 Var4 Var5
[1,] TRUE FALSE FALSE FALSE FALSE
[2,] FALSE TRUE FALSE FALSE FALSE
[3,] TRUE TRUE FALSE FALSE FALSE
[4,] FALSE FALSE TRUE FALSE FALSE
[5,] TRUE FALSE TRUE FALSE FALSE
[6,] FALSE TRUE TRUE FALSE FALSE
Преобразование матрицы в список индексов столбцов
indexes <- lapply(seq_len(nrow(v)), function(x) v[x, ])
names(indexes) <- apply(v, 1, function(x) paste(names(df)[x], collapse="."))
К сожалению, я застрял здесь.
Мне нужно сгенерировать 27 фиктивных переменных (32-5) в соответствии свышеупомянутые комбинации.
EDT: я решил проблему с этим грязным способом:
# my df
set.seed(101)
df <- data.frame(
alfa = sample(c(0, 1), 30, replace = TRUE),
beta = sample(c(0, 1), 30, replace = TRUE),
gamma = sample(c(0, 1), 30, replace = TRUE),
delta = sample(c(0, 1), 30, replace = TRUE),
epsilon = sample(c(0, 1), 30, replace = TRUE)
)
# count the numebr of coesistent conditions:
df$n <- rowSums(df[1:5], na.rm = TRUE)
# Dirty way to compute multiple combinations:
df$alfa.beta <- ifelse (df$alfa == 1 & df$beta == 1 & df$gamma == 0 & df$delta == 0 & df$epsilon == 0 , 1, 0)
df$alfa.gamma <- ifelse (df$alfa == 1 & df$beta == 0 & df$gamma == 1 & df$delta == 0 & df$epsilon == 0 , 1, 0)
df$beta.gamma <- ifelse (df$alfa == 0 & df$beta == 1 & df$gamma == 1 & df$delta == 0 & df$epsilon == 0 , 1, 0)
df$alfa.beta.gamma <- ifelse (df$alfa == 1 & df$beta == 1 & df$gamma == 1 & df$delta == 0 & df$epsilon == 0 , 1, 0)
df$alfa.delta <- ifelse (df$alfa == 1 & df$beta == 0 & df$gamma == 0 & df$delta == 1 & df$epsilon == 0 , 1, 0)
df$beta.delta <- ifelse (df$alfa == 0 & df$beta == 1 & df$gamma == 0 & df$delta == 1 & df$epsilon == 0 , 1, 0)
df$alfa.beta.delta <- ifelse (df$alfa == 1 & df$beta == 1 & df$gamma == 0 & df$delta == 1 & df$epsilon == 0 , 1, 0)
df$gamma.delta <- ifelse (df$alfa == 0 & df$beta == 0 & df$gamma == 1 & df$delta == 1 & df$epsilon == 0 , 1, 0)
df$alfa.gamma.delta <- ifelse (df$alfa == 1 & df$beta == 0 & df$gamma == 1 & df$delta == 1 & df$epsilon == 0 , 1, 0)
df$beta.gamma.delta <- ifelse (df$alfa == 0 & df$beta == 1 & df$gamma == 1 & df$delta == 1 & df$epsilon == 0 , 1, 0)
df$alfa.beta.gamma.delta <- ifelse (df$alfa == 1 & df$beta == 1 & df$gamma == 1 & df$delta == 1 & df$epsilon == 0 , 1, 0)
df$alfa.epsilon <- ifelse (df$alfa == 1 & df$beta == 0 & df$gamma == 0 & df$delta == 0 & df$epsilon == 1 , 1, 0)
df$beta.epsilon <- ifelse (df$alfa == 0 & df$beta == 1 & df$gamma == 0 & df$delta == 0 & df$epsilon == 1 , 1, 0)
df$alfa.beta.epsilon <- ifelse (df$alfa == 1 & df$beta == 1 & df$gamma == 0 & df$delta == 0 & df$epsilon == 1 , 1, 0)
df$gamma.epsilon <- ifelse (df$alfa == 0 & df$beta == 0 & df$gamma == 1 & df$delta == 0 & df$epsilon == 1 , 1, 0)
df$alfa.gamma.epsilon <- ifelse (df$alfa == 1 & df$beta == 0 & df$gamma == 1 & df$delta == 0 & df$epsilon == 1 , 1, 0)
df$beta.gamma.epsilon <- ifelse (df$alfa == 0 & df$beta == 1 & df$gamma == 1 & df$delta == 0 & df$epsilon == 1 , 1, 0)
df$alfa.beta.gamma.epsilon <- ifelse (df$alfa == 1 & df$beta == 1 & df$gamma == 1 & df$delta == 0 & df$epsilon == 1 , 1, 0)
df$delta.epsilon <- ifelse (df$alfa == 0 & df$beta == 0 & df$gamma == 0 & df$delta == 1 & df$epsilon == 1 , 1, 0)
df$alfa.delta.epsilon <- ifelse (df$alfa == 1 & df$beta == 0 & df$gamma == 0 & df$delta == 1 & df$epsilon == 1 , 1, 0)
df$beta.delta.epsilon <- ifelse (df$alfa == 0 & df$beta == 1 & df$gamma == 0 & df$delta == 1 & df$epsilon == 1 , 1, 0)
df$alfa.beta.delta.epsilon <- ifelse (df$alfa == 1 & df$beta == 1 & df$gamma == 0 & df$delta == 1 & df$epsilon == 1 , 1, 0)
df$gamma.delta.epsilon <- ifelse (df$alfa == 0 & df$beta == 0 & df$gamma == 1 & df$delta == 1 & df$epsilon == 1 , 1, 0)
df$alfa.gamma.delta.epsilon <- ifelse (df$alfa == 1 & df$beta == 0 & df$gamma == 1 & df$delta == 1 & df$epsilon == 1 , 1, 0)
df$beta.gamma.delta.epsilon <- ifelse (df$alfa == 0 & df$beta == 1 & df$gamma == 1 & df$delta == 1 & df$epsilon == 1 , 1, 0)
df$alfa.beta.gamma.delta.epsilon <- ifelse (df$alfa == 1 & df$beta == 1 & df$gamma == 1 & df$delta == 1 & df$epsilon == 1 , 1, 0)