rbindlist, data.frame, :
library(data.table)
n <- 1L
setDT(company_data)[order(-Users), .SD[n], keyby=Company]
setDTconverts data.frameto data.tableby reference (without using additional copy / memory). Then we sort the data table in descending order on the column Users, and then group by company, and for each group we get the nith row from S ubset D ata ( .SD) for this group.
In your case, maybe
DT <- rbindlist(selected)
DT[order(-Users), .SD[n], keyby=Company]
But the previous solution is much more efficient and easy to use with one layer to solve the problem.
data
company_data <- structure(list(Company = c("MSFT", "MSFT", "GOOG", "GOOG", "MSFT",
"APPL", "APPL"), Product = c("Office", "VS", "gmail", "appengine",
"Windows", "iOS", "iCloud"), Users = c(1000L, 4000L, 3203L, 45454L,
1500L, 6000L, 3442L)), .Names = c("Company", "Product", "Users"
), class = "data.frame", row.names = c(NA, -7L))