fork(1) download
  1. myuniq <- function(df){
  2. if(NROW(df)<2){
  3. return(df)
  4. } else {
  5. uniq=!apply(apply(df,2,"%in%",df[1,]),1,any)
  6. return(rbind(df[1,],myuniq(df[uniq,])))
  7. }
  8. }
  9.  
  10. another_unique <- function(df){
  11. if(nrow(df)<2){
  12. return(df)
  13. }
  14.  
  15. list_x = c(df$x)
  16. list_y = c(df$y)
  17. list_all = c()
  18. list_x_new = c()
  19. list_y_new = c()
  20. for(x in 1:length(list_x)){
  21. if(list_x[x] %in% list_all | list_y[x] %in% list_all){
  22.  
  23. }else{
  24. list_all = c(list_all, list_x[x], list_y[x])
  25. list_x_new = c(list_x_new, list_x[x])
  26. list_y_new = c(list_y_new, list_y[x])
  27. }
  28. }
  29. return(data.frame(x=list_x_new, y=list_y_new))
  30.  
  31. }
  32.  
  33. set.seed(123456)
  34. n=10000
  35. df=data.frame(x=sample(1:500,n,replace = T)
  36. ,y=sample(1:500,n,replace = T))
  37.  
  38.  
  39. # 測測看
  40. ss <- Sys.time()
  41. new_df_a <- another_unique(df)
  42. print(paste('another_unique:', Sys.time() - ss))
  43. summary(new_df_a)
  44.  
  45. # showfeb大範例
  46. ss <- Sys.time()
  47. new_df <- myuniq(df)
  48. print(paste('myuniq:', Sys.time() - ss))
  49. summary(new_df)
  50.  
  51. print(sum(new_df_a == new_df) / 2 == nrow(new_df))
  52.  
Success #stdin #stdout 2.56s 63212KB
stdin
Standard input is empty
stdout
[1] "another_unique: 0.0587425231933594"
       x               y        
 Min.   :  1.0   Min.   :  2.0  
 1st Qu.:130.0   1st Qu.:129.5  
 Median :262.0   Median :240.0  
 Mean   :250.7   Mean   :252.8  
 3rd Qu.:369.5   3rd Qu.:390.0  
 Max.   :497.0   Max.   :500.0  
[1] "myuniq: 2.29200959205627"
       x               y        
 Min.   :  1.0   Min.   :  2.0  
 1st Qu.:130.0   1st Qu.:129.5  
 Median :262.0   Median :240.0  
 Mean   :250.7   Mean   :252.8  
 3rd Qu.:369.5   3rd Qu.:390.0  
 Max.   :497.0   Max.   :500.0  
[1] TRUE