2016-12-20 7 views
2

ich einen Datenrahmen im Format haben:entfernen benachbarte Duplikate in r

site_domain <- c('ebay.com','facebook.com','facebook.com','ebay.com','ebay.com','auto.com','ebay.com','facebook.com','auto.com','ebay.com','facebook.com','facebook.com','ebay.com','facebook.com','auto.com','auto.com') 
id <- c(1, 1, 1,2,2,3,3,3,3,4,4,4,5,5,5,5) 
file0 <- as.data.frame(cbind(site_domain,id)) 

ich eine Gruppe tat, indem Sie auf "id" die Daten zu erhalten:

library(dplyr) 
xx <- as.data.frame(file0 %>% 
         group_by(id) %>% 
         summarise(pages=paste(site_domain, collapse='_'))) 

Die Daten wie folgt aussieht:

1 ebay.com_facebook.com_facebook.com 
2 ebay.com_ebay.com 
3 auto.com_ebay.com_facebook.com_auto.com 
4 ebay.com_facebook.com_facebook.com 
5 ebay.com_facebook.com_auto.com_auto.com 

Allerdings möchte ich neben Duplikate entfernen, so will ich löschte wie:

1 ebay.com_facebook.com 
2 ebay.com 
3 auto.com_ebay.com_facebook.com_auto.com 
4 ebay.com_facebook.com 
5 ebay.com_facebook.com_auto.com 

Wie kann ich dies erreichen.

Antwort

2

Wir values verwenden können Eigenschaft von rle, um benachbarte Duplikate zu entfernen.

library(dplyr) 
file0 %>% 
    group_by(id) %>% 
    summarise(pages=paste(rle(as.character(site_domain))$values, collapse='_')) 

#  id         pages 
# <fctr>         <chr> 
#1  1     ebay.com_facebook.com 
#2  2        ebay.com 
#3  3 auto.com_ebay.com_facebook.com_auto.com 
#4  4     ebay.com_facebook.com 
#5  5   ebay.com_facebook.com_auto.com 
1

Mit unique Funktion:

xx <- as.data.frame(file0 %>% 
         group_by(id) %>% 
         summarise(pages=paste(unique(site_domain), collapse='_'))) 

xx 

# id       pages 
#1 1   ebay.com_facebook.com 
#2 2      ebay.com 
#3 3 auto.com_ebay.com_facebook.com 
#4 4   ebay.com_facebook.com 
#5 5 ebay.com_facebook.com_auto.com      
+0

das sieht gut aus, Allerdings sehe ich in der 3. Zeile: Ausgabe sollte 'auto.com_ebay.com_facebook.com_auto.com' sein, aber dieser Ansatz ergibt:' aut o.com_ebay.com_facebook.com' .. jede Hilfe, wie können wir das ändern. – PSraj

+0

Ja, es ist möglich, ich werde die Antwort mit dem neuen Ansatz – OdeToMyFiddle

1

es ist leicht, die Doppelarbeit zu entfernen, bevor

 file0 <- file0 [!duplicated(file0),] 


     site_domain id 
     1  ebay.com 1 
     2 facebook.com 1 
     4  ebay.com 2 
     6  auto.com 3 
     7  ebay.com 3 
     8 facebook.com 3 
     10  ebay.com 4 
     11 facebook.com 4 
     13  ebay.com 5 
     14 facebook.com 5 
     15  auto.com 5 

Gruppierung, dann können Sie gruppieren die Daten von id

  id       pages 
      1 1   ebay.com_facebook.com 
      2 2      ebay.com 
      3 3 auto.com_ebay.com_facebook.com 
      4 4   ebay.com_facebook.com 
      5 5 ebay.com_facebook.com_auto.com 
+0

aktualisieren, auch dies kommt in der Nähe, aber Ausgang sollte sein: 'auto.com_ebay.com_facebook.com_auto.com' aber dieser Ansatz gibt auch:' auto.com_ebay.com_facebook.com'. Irgendwelche Hilfe, wie können wir das ändern? – PSraj

2

Hier ist eine Option mit data.table

library(data.table) 
setDT(file0)[, unique(site_domain), by= .(id, grp=rleid(site_domain)) 
      ][, .(site=paste(V1, collapse="_")) , id] 
# id         site 
#1: 1     ebay.com_facebook.com 
#2: 2        ebay.com 
#3: 3 auto.com_ebay.com_facebook.com_auto.com 
#4: 4     ebay.com_facebook.com 
#5: 5   ebay.com_facebook.com_auto.com 

Oder einen Index mit .I erstellen, extrahieren Sie die Zeilen und paste von 'id'

i1 <- setDT(file0)[, .I[!duplicated(site_domain)], .(id, grp = rleid(site_domain))]$V1 
file0[i1, .(site = paste(site_domain, collapse="_")), by = id]