这是 dplyr distinct()
泛型的方法。它被翻译为 data.table::unique.data.table()
。
参数
- .data
- ...
-
<
data-masking
> 确定唯一性时使用的可选变量。如果给定的输入组合有多行,则仅保留第一行。如果省略,将使用 DataFrame 中的所有变量。 - .keep_all
-
如果是
TRUE
,则将所有变量保留在.data
中。如果...
的组合不不同,则保留第一行值。
例子
library(dplyr, warn.conflicts = FALSE)
df <- lazy_dt(data.frame(
x = sample(10, 100, replace = TRUE),
y = sample(10, 100, replace = TRUE)
))
df %>% distinct(x)
#> Source: local data table [10 x 1]
#> Call: unique(`_DT7`[, .(x)])
#>
#> x
#> <int>
#> 1 10
#> 2 4
#> 3 1
#> 4 5
#> 5 7
#> 6 2
#> # … with 4 more rows
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
df %>% distinct(x, y)
#> Source: local data table [64 x 2]
#> Call: unique(`_DT7`)
#>
#> x y
#> <int> <int>
#> 1 10 8
#> 2 4 3
#> 3 1 8
#> 4 1 6
#> 5 5 7
#> 6 7 10
#> # … with 58 more rows
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
df %>% distinct(x, .keep_all = TRUE)
#> Source: local data table [10 x 2]
#> Call: unique(`_DT7`, by = "x")
#>
#> x y
#> <int> <int>
#> 1 10 8
#> 2 4 3
#> 3 1 8
#> 4 5 7
#> 5 7 10
#> 6 2 7
#> # … with 4 more rows
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
相关用法
- R dtplyr drop_na.dtplyr_step 删除包含缺失值的行
- R dtplyr lazy_dt 创建一个“惰性”data.table 以与 dplyr 动词一起使用
- R dtplyr group_modify.dtplyr_step 对每个组应用一个函数
- R dtplyr transmute.dtplyr_step 创建新列,删除旧列
- R dtplyr slice.dtplyr_step 使用行的位置对行进行子集化
- R dtplyr left_join.dtplyr_step 连接数据表
- R dtplyr fill.dtplyr_step 用上一个或下一个值填充缺失值
- R dtplyr filter.dtplyr_step 使用列值对行进行子集化
- R dtplyr mutate.dtplyr_step 创建和修改列
- R dtplyr unite.dtplyr_step 通过将字符串粘贴在一起将多列合并为一列。
- R dtplyr nest.dtplyr_step 巢
- R dtplyr relocate.dtplyr_step 使用变量名称重新定位变量
- R dtplyr head.dtplyr_step 对第一行或最后一行进行子集化
- R dtplyr expand.dtplyr_step 扩展 DataFrame 以包含所有可能的值组合。
- R dtplyr group_by.dtplyr_step 分组和取消分组
- R dtplyr intersect.dtplyr_step 设置操作
- R dtplyr pivot_wider.dtplyr_step 将数据从长轴转向宽轴
- R dtplyr summarise.dtplyr_step 将每组汇总为一行
- R dtplyr count.dtplyr_step 按组计数观察值
- R dtplyr select.dtplyr_step 使用名称对列进行子集化
- R dtplyr complete.dtplyr_step 完成缺少数据组合的 DataFrame
- R dtplyr collect.dtplyr_step 强制计算惰性 data.table
- R dtplyr arrange.dtplyr_step 按列值排列行
- R dtplyr separate.dtplyr_step 使用正则表达式或数字位置将字符列分成多列
- R dtplyr rename.dtplyr_step 使用名称重命名列
注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Subset distinct/unique rows。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。