在 R data.table 中保留多列的第一行 [英] Keep first row by multiple columns in an R data.table
本文介绍了在 R data.table 中保留多列的第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想从 data.table 中获取第一行,按多列分组.
I'd like to get the first row only from a data.table, grouped by multiple columns.
这很简单,只有一列,例如:
This is straightforward with a single column, e.g.:
(dt <- data.table(x = c(1, 1, 1, 2),
y = c(1, 1, 2, 2),
z = c(1, 2, 1, 2)))
# x y z
# |1: 1 1 1
# |2: 1 1 2
# |3: 1 2 1
# |4: 2 2 2
dt[!duplicated(x)] # Remove rows 2-3
# x y z
# |1: 1 1 1
# |2: 2 2 2
但是当尝试基于两列进行删除时,这些方法都不起作用;即在这种情况下仅删除第 2 行:
But none of these approaches work when trying to remove based on two columns; i.e. in this case removing only row 2:
dt[!duplicated(x, y)] # Keeps only original data set
# x y z
# |1: 1 1 1
# |2: 1 1 2
# |3: 1 2 1
# |4: 2 2 2
dt[!duplicated(list(x, y))] # Same as above
dt[!duplicated(c("x", "y"))] # Same as above
dt[!duplicated(list("x", "y"))] # Same as above
dt[!duplicated(c(x, y))] # Only removes duplicates from first column
# x y z
# |1: 1 1 1
# |2: 2 2 2
除了这个,它只在某些情况下有效:
Except for this, which only works in certain cases:
dt[!duplicated(paste0(x, y))]
# x y z
# |1: 1 1 1
# |2: 1 2 1
# |3: 2 2 2
推荐答案
data.table
为 unique
、duplicated
和 duplicated
提供 S3 方法code>anyDuplicated
data.table
provides S3 methods for unique
, duplicated
and anyDuplicated
unique(dt, by = c('x','y'))
会给你你想要的.
这篇关于在 R data.table 中保留多列的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文