First of all: you can get the speed up using:
idx <- sapply(split(seq_len(nrow(d)), d$label), function(x) {
x[which.max(d$value[x])]})
For a 100k data.frame
, on my machine it is 5x faster than d[x,"value"]
version.
For a large data.frame
and many labels you could use a similar method that I posted in earlier question:
dd <- d[i<-order(d$label, d$value),] # dd is sorted by label and value
ind <- c(dd$label[-1] != dd$label[-n], TRUE)
idx <- setNames(seq_len(nrow(d))[i][ind], dd$label[ind])
edit: A more efficient solution with the use of a trick from Martin Morgan answer:
v <- d$label[i<-order(d$value)] # we need only label, and with Martin
# trick sorting over label is not needed
ind <- !duplicated(v, fromLast=TRUE) # it finds last (max) occurrence of label
idx <- setNames(seq_len(nrow(d))[i][ind], v[ind])
NOTE: order of final vector is different.
It depends on your actual data structure but you should gain a nice speed-up:
Timings:
# NOTE: different machine, so timing differ from previous
set.seed(6025051)
n <- 100000; k <- 20000
d <- data.frame(value=rnorm(n),
label=sample(paste("A",seq_len(k),sep="_"), n, replace=TRUE))
system.time(
idx_1 <- sapply(split(1:nrow(d), d$label), function(x) {
x[which.max(d[x,"value"])]})
)
# user system elapsed
# 1.30 0.02 1.31
system.time(
idx_1b <- sapply(split(seq_len(nrow(d)), d$label), function(x) {
x[which.max(d$value[x])]})
)
# user system elapsed
# 0.23 0.00 0.23
all.equal(idx_1, idx_1b)
# [1] TRUE
system.time({
dd <- d[i<-order(d$label, d$value),]
ind <- c(dd$label[-1] != dd$label[-n], TRUE)
idx_2 <- setNames(seq_len(nrow(d))[i][ind],dd$label[ind])
})
# user system elapsed
# 0.19 0.00 0.19
all.equal(idx_1, idx_2)
# [1] TRUE
new solution
system.time({
v <- d$label[i<-order(d$value)]
ind <- !duplicated(v, fromLast=TRUE)
idx_3 <- setNames(seq_len(nrow(d))[i][ind], v[ind])
})
# user system elapsed
# 0.05 0.00 0.04
all.equal(sort(idx_1), sort(idx_3))
# [1] TRUE