跟进我的问题 here ,我试图在 R 中复制 Stata 命令的功能 duplicates tag
,这允许我标记数据集的所有行,这些行就给定的变量集而言是重复的:
clear *
set obs 16
g f1 = _n
expand 104
bys f1: g f2 = _n
expand 2
bys f1 f2: g f3 = _n
expand 41
bys f1 f2 f3: g f4 = _n
des // describe the dataset in memory
preserve
sample 10 // draw a 10% random sample
tempfile sampledata
save `sampledata', replace
restore
// append the duplicate rows to the data
append using `sampledata'
sort f1-f4
duplicates tag f1-f4, generate(dupvar)
browse if dupvar == 1 // check that all duplicate rows have been tagged
编辑
这是 Stata 生成的内容(根据@Arun 的要求添加):
f1 f2 f3 f4 dupvar 1 1 1 1 0 1 1 1 2 0 1 1 1 3 1 1 1 1 3 1 1 1 1 4 0 1 1 1 5 0 1 1 1 6 0 1 1 1 7 0 1 1 1 8 1 1 1 1 8 1
请注意,对于
(f1, f2, f3, f4) = (1, 1, 1, 3)
有两行,这两行都标有 dupvar = 1
.同样,对于 (f1, f2, f3, f4) =(1, 1, 1, 8)
重复的两行.回复:
基函数
duplicated
仅标记第二个重复项。所以,我写了一个函数来复制 R 中的 Stata 功能,使用 ddply
.# Values of (f1, f2, f3, f4) uniquely identify observations
dfUnique = expand.grid(f1 = factor(1:16),
f2 = factor(1:41),
f3 = factor(1:2),
f4 = factor(1:104))
# sample some extra rows and rbind them
dfDup = rbind(dfUnique, dfUnique[sample(1:nrow(dfUnique), 100), ])
# dummy data
dfDup$data = rnorm(nrow(dfDup))
# function: use ddply to tag all duplicate rows in the data
fnDupTag = function(dfX, indexVars) {
dfDupTag = ddply(dfX, .variables = indexVars, .fun = function(x) {
if(nrow(x) > 1) x$dup = 1 else x$dup = 0
return(x)
})
return(dfDupTag)
}
# test the function
indexVars = paste0('f', 1:4, sep = '')
dfTemp = fnDupTag(dfDup, indexVars)
但就像在链接的问题中一样,性能是一个巨大的问题。 Another possible solution是
dfDup$dup = duplicated(dfDup[, indexVars]) |
duplicated(dfDup[, indexVars], fromLast = TRUE)
dfDupSorted = with(dfDup, dfDup[order(eval(parse(text = indexVars))), ])
我有几个问题:
1.是否可以制作
ddply
版本更快?2.是第二个版本使用
duplicated
正确的?对于重复行的两个以上副本?3. 我将如何使用
data.table
来做到这一点?那会更快吗?
最佳答案
我会在这里回答你的第三个问题..(我认为第一个问题或多或少在你的 other post 中得到了回答)。
## Assuming DT is your data.table
DT[, dupvar := 1L*(.N > 1L), by=c(indexVars)]
:=
添加新列 dupvar
通过引用(因此非常快,因为没有复制)。 .N
是 data.table
中的一个特殊变量,它提供了属于每个组的观察数(这里,对于每个 f1,f2,f3,f4
)。慢慢来,通过
?data.table
(并在那里运行示例)以了解用法。以后会为你节省很多时间。所以,基本上,我们按
indexVars
分组, 检查是否 .N > 1L
如果是这样,它会返回 TRUE
.我们乘以 1L
返回 integer
而不是 logical
值(value)。如果需要,您还可以使用
setkey
按列对它进行排序。 .从下一个版本开始(目前在v1.9.3 - 开发版中实现),还有一个功能
setorder
导出的只是对 data.table
进行排序供引用,无需设置 key 。它也可以按升序或降序排序。 (请注意,setkey
始终仅按升序排序)。也就是说,在下一个版本中,您可以执行以下操作:
setorder(DT, f1, f2, f3, f4)
## or equivalently
setorderv(DT, c("f1", "f2", "f3", "f4"))
另外,用法
DT[order(...)]
也在内部优化以使用 data.table
的快速订购。即,DT[order(...)]
内部检测到并更改为 DT[forder(DT, ...)]
这比 base 的 order
快得难以置信.因此,如果您不想通过引用更改它,并且想要分配已排序的 data.table
到另一个变量,你可以这样做:DT_sorted <- DT[order(f1, f2, f3, f4)] ## internally optimised for speed
## but still copies!
HTH
关于r - 像在 Stata 中一样标记 R 中的所有重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22893028/