简体   繁体   中英

R data.table fast grouping for different column

Maybe someone can point me in the right direction for this. I can't seem to find an easy solution for this problem

I have a data.table as given here.

library(data.table)
dtData <- data.table(DateTime = c(1,2,1,2,3, 1,2,3,4), 
                     Id = c(1,1,2,2,2,3,3,3,3), 
                     LastTrade = as.Date(c('2013-01-01', '2013-01-01', '2013-06-01', 
                                           '2013-06-01', '2013-06-01', '2013-09-01',
                                           '2013-09-01', '2013-09-01', '2013-09-01')))

I would like to do a fast grouping of the data. So I can easily do:

dtData[, min(LastTrade), by=DateTime]

which gives me

    DateTime        V1
1:        1 2013-01-01
2:        2 2013-01-01
3:        3 2013-06-01
4:        4 2013-09-01

Now my question: How can I, instead of getting back the minimum LastTrade column as "V1", get the "Id" column as the result?

    DateTime    V1
1:        1      1
2:        2      1
3:        3      2
4:        4      3

You can use which.min to identify the row containing the minimum, and use it to subset the Id column.

dtData[, Id[which.min(LastTrade)], by=DateTime]
#    DateTime V1
# 1:        1  1
# 2:        2  1
# 3:        3  2
# 4:        4  3

I'd make use of the "key" feature of data.table and then use mult="first" option. This'll remove the necessity for a "min" call for each group and should be faster.

# sort by DateTime and LastTrade once
setkey(dtData, DateTime, LastTrade)
dtData[J(unique(DateTime)), mult="first"]
   DateTime Id  LastTrade
1:        1  1 2013-01-01
2:        2  1 2013-01-01
3:        3  2 2013-06-01
4:        4  3 2013-09-01

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM