【发布时间】:2018-09-18 12:49:47
【问题描述】:
我有一个包含 1,000 多列和数十万行的 tibble。我想摆脱重复值,同时为每一行保留唯一的 ID 值。这是我尝试使用 mtcars 的简化版本。
library(tidyverse)
mtcars %>%
as_tibble() %>%
rownames_to_column() %>%
distinct(mpg:carb, .keep_all = TRUE)
#Error in mutate_impl(.data, dots) :
# Column `mpg:carb` must be length 32 (the number of rows) or one, not 18
#In addition: Warning messages:
#1: In mpg:carb : numerical expression has 32 elements: only the first used
#2: In mpg:carb : numerical expression has 32 elements: only the first used
任何想法如何在保留 ID 变量的同时删除非唯一行?在 mtcars 示例中,ID 变量是 rownames。列太多,我无法单独键入。
【问题讨论】:
-
顺便说一句,我什至不确定 mtcars 是否有任何重复项...将来,请尝试使用更简单的示例,例如:
dd<-data.frame(a=c("a","b","c","d","e","f"), b=c(1,1:5), c=c(1,1,3,2,4,5))