Another way using which.max
in lapply
to subset df$Group
.
cbind(lapply(df[-1], \(x) df$Group[which.max(x)]))
# [,1]
#Sample1 "Cluster2"
#Sample2 "Cluster3"
#Sample3 "Cluster3"
Or using vapply
.
cbind(vapply(df[-1], \(x) df$Group[which.max(x)], ""))
Or using the index and create a data.frame
.
data.frame(Sample = names(df)[-1],
Group = df$Group[vapply(df[-1], which.max, 0L)])
# Sample Group
#1 Sample1 Cluster2
#2 Sample2 Cluster3
#3 Sample3 Cluster3
Benchmark
library(dplyr)
library(tidyr)
bench::mark(check = FALSE,
"Darren Tsai" = {df %>%
pivot_longer(-Group) %>%
slice_max(value, by = name)},
Sotos = {df %>%
pivot_longer(-1) %>%
group_by(name) %>%
summarise(Group = Group[value == max(value)])},
GuedesBF = {df |> summarise(across(starts_with("sample"),
~Group[which.max(.x)])) |>
pivot_longer(everything())},
ThomasIsCoding = data.frame(
Sample = names(df)[-1],
Group = df$Group[max.col(t(df[-1]))]
),
GKi1 = cbind(lapply(df[-1], \(x) df$Group[which.max(x)])),
GKi2 = cbind(vapply(df[-1], \(x) df$Group[which.max(x)], "")),
GKi3 = data.frame(Sample = names(df)[-1],
Group = df$Group[vapply(df[-1], which.max, 0L)])
)
Result
expression min median `itr/sec` mem_alloc `gc/sec` n_itr n_gc
<bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl> <int> <dbl>
1 Darren Tsai 5.66ms 5.74ms 173. 3.31MB 8.55 81 4
2 Sotos 6.42ms 6.56ms 152. 1.11MB 11.1 68 5
3 GuedesBF 5.81ms 5.91ms 169. 402.38KB 8.55 79 4
4 ThomasIsCoding 292.09µs 308.49µs 3204. 96.38KB 10.3 1563 5
5 GKi1 35.3µs 38.89µs 25198. 11.1KB 12.6 9995 5
6 GKi2 36.09µs 39.67µs 24528. 7.96KB 14.7 9994 6
7 GKi3 215.37µs 225.92µs 4348. 0B 10.2 2123 5