Вот подход с Reduce()
n_cols <- length(df_1)
Reduce(`&`,
lapply(seq_len(n_cols - 1),
function(j) df_1[[j]] == df_1[[j+1]])
)
Вот производительность некоторых ответов с оценкой TRUE
или FALSE
:
# A tibble: 4 x 13
expression min median
<bch:expr> <bch:t> <bch:t>
1 Reduce_way 47.7us 50.5us
2 rowSums(df_1[, -1] == df_1[, 1]) == length(df_1[, -1]) 159.6us 168.6us
3 apply(df_1, 1, function(x) length(unique(x)) == 1) 150.6us 158.1us
4 df_1[[1]] == df_1[[2]] & df_1[[2]] == df_1[[3]] 27.5us 29.6us
Производительность зависитна количество оцениваемых столбцов и строк. Например, 100 000 x 3:
df_1 <- as.data.frame(replicate(3, sample(3, 100000, replace = T)))
expression min median
<bch:expr> <bch:tm> <bch:t>
1 Reduce_way 931.5us 1.13ms
2 rowSums(df_1[, -1] == df_1[, 1]) == length(df_1[, -1]) 10.96ms 12.69ms
3 apply(df_1, 1, function(x) length(unique(x)) == 1) 1.01s 1.01s
4 df_1[[1]] == df_1[[2]] & df_1[[2]] == df_1[[3]] 894.8us 1.06ms
# following is used from here on out instead of writing out df_1[[1]] == ...
n_cols <- length(df_1)
eval_parse <- paste(
apply(matrix(rep(seq_len(n_cols), c(1, rep(2, n_cols - 2), 1)), 2),
2,
function(cols) paste0("df_1[[", cols, "]]", collapse = ' == ')
),
collapse = ' & '
)
## for 100 x 1000 data.frame
df_1 <- as.data.frame(replicate(1000, sample(3, 100, replace = T)))
# A tibble: 4 x 13
expression min median `itr/sec`
<bch:expr> <bch:> <bch:> <dbl>
1 Reduce_way 15.9ms 16.3ms 60.9
2 rowSums(df_1[, -1] == df_1[, 1]) == length(df_1[, -1]) 16.5ms 17.1ms 58.1
3 apply(df_1, 1, function(x) length(unique(x)) == 1) 10.4ms 10.7ms 92.4
4 eval(parse(text = eval_parse)) 20.1ms 20.6ms 47.4