2017-05-04 22 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