Bra*_*rad 16 r self-join data.table
我正在尝试使用data.table通过一组三个变量获得最高行.
我有一个有效的解决方案:
col1 <- c(1,1,1,1,2,2,2,2,3,3,3,3)
col2 <- c(2000,2000,2001,2001,2000,2000,2001,2001,2000,2000,2001,2001)
col4 <- c(1,2,3,4,5,6,7,8,9,10,11,12)
data <- data.frame(store=col1,year=col2,month=12,sales=col4)
solution1 <- data.table(data)[,.SD[1,],by="store,year,month"]
Run Code Online (Sandbox Code Playgroud)
我使用Matthew Dowle在以下链接中建议的较慢方法:
我正在尝试实现更快的自联接但无法使其工作.
有没有人有什么建议?
mne*_*nel 23
设置密钥 store, year, month
DT <- data.table(data, key = c('store','year','month'))
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用unique创建包含键列的唯一值的data.table.默认情况下,这将采用第一个条目
unique(DT)
store year month sales
1: 1 2000 12 1
2: 1 2001 12 3
3: 2 2000 12 5
4: 2 2001 12 7
5: 3 2000 12 9
6: 3 2001 12 11
Run Code Online (Sandbox Code Playgroud)
但是,可以肯定的是,你可以使用自联接mult='first'.(其他选项是'all'或'last')
# the key(DT) subsets the key columns only, so you don't end up with two
# sales columns
DT[unique(DT[,key(DT), with = FALSE]), mult = 'first']
Run Code Online (Sandbox Code Playgroud)
如果没有设置按键,这将是更快地使用.I 不.SD
DTb <- data.table(data)
DTb[DTb[,list(row1 = .I[1]), by = list(store, year, month)][,row1]]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6223 次 |
| 最近记录: |