2017-05-04 39 views

回答

0

我不知道这是否会更快,但你可以使用distinctdplyr这样的:

df %>% distinct(x)

另一种选择是使用group_by(也是从dplyr):

df %>% group_by(x)

0

检查data.table包。这里是你可以做什么:

set.seed(1) 
df <- data.frame(col1 = sample(x = 5000, size = 1e6, replace = TRUE), 
       col2 = sample(x = 5000, size = 1e6, replace = TRUE)) 
dt <- copy(df) 
setDT(dt) #here you convert a data.frame object into a data.table one by reference 

unique(dt) 

我们可以检查使用microbenchmark包时间增益:

microbenchmark(unique(df), unique(dt)) 

# Unit: milliseconds 
#  expr  min   lq  mean median  uq  max neval 
# unique(df) 1028.92260 1285.39321 1410.4072 1405.7486 1543.1486 1857.4901 100 
# unique(dt) 83.11946 98.41596 148.0874 119.0889 155.0679 507.4944 100