I have a data table, dt, that looks like
location year value NYC 2026 1 NYC 2026 2 NYC 2026 3 NYC 2026 4 NYC 2026 5 LA 2026 6 LA 2026 7 LA 2026 8 LA 2026 9 LA 2026 10 I would like to group them by city and year and find the 2nd smallest element in the column value, per each group, where the desired result looks like:
location year value NYC 2026 2 LA 2026 7 dt %>% grou_by(location, year) %>% nth(value, 2)
Would not work. Any help is appreciated.
The data table above can be created by:
dt <- structure(list(location = c("NYC", "NYC", "NYC","NYC", "NYC", "LA", "LA", "LA", "LA", "LA"), year = c(2026, 2026, 2026, 2026, 2026, 2026, 2026, 2026, 2026, 2026), value = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), class = "data.table", row.names = c(NA, -10L))