2017-01-23 4 views
0

Ich bin mit R und versuchen Zeilen in einem Datenrahmen mit einem Spaltenwert unterschiedlich, aber andere 4 Spaltenwerte derselben zu finden:R eine Spalte gleich, aber andere Spalte finden Duplikate

I.E.

rn c1 c2 c3 c4 c5 c6 
1 t a b c d e 
2 f a b c d g 
3 t 1 2 3 4 5 
4 t 1 2 3 4 5 
5 t 1 2 3 4 5 
6 f a b c d e 
7 f a b c d e 
8 t a b c d e 

Also in diesem Fall nur die Zeilen, die: 1. haben doppelte Werte in Spalte c2-c5 2. aber auch unterschiedlichen Wert in c1 bleiben.

rn c1 c2 c3 c4 c5 c6 
1 t a b c d e 
2 f a b c d g 
6 f a b c d e 
7 f a b c d e 
8 t a b c d e 

Wie auch immer?

Dank

+0

Es scheint mir, dass Ihre Frage zu den Reihen kommt nach unten, die den ersten Satz fester Zeile duplizieren s und unterscheiden sich von ihren Vorgängern? Ist das richtig? – JWLM

Antwort

0

Wir data.table verwenden können. Wandle den 'data.frame' in 'data.table' um (setDT(df1)), gruppiert nach den Spalten c2 bis c5 (names(df1)[3:6]), der unique (uniqueN) Elemente von 'c1' ist größer als 1, wir unterteilen die data.table (.SD)

library(data.table) 
setDT(df1)[, if(uniqueN(c1)>1) .SD, c(names(df1)[3:6])][, names(df1), with= FALSE] 
# rn c1 c2 c3 c4 c5 c6 
#1: 1 t a b c d e 
#2: 2 f a b c d g 
#3: 6 f a b c d e 
#4: 7 f a b c d e 
#5: 8 t a b c d e 

Eine entsprechende Option in dplyr ist

library(dplyr) 
df1 %>% 
    group_by_(.dots = names(df1)[3:6]) %>% 
    filter(n_distinct(c1) > 1) 
#  rn c1 c2 c3 c4 c5 c6 
# <int> <chr> <chr> <chr> <chr> <chr> <chr> 
#1  1  t  a  b  c  d  e 
#2  2  f  a  b  c  d  g 
#3  6  f  a  b  c  d  e 
#4  7  f  a  b  c  d  e 
#5  8  t  a  b  c  d  e 
Verwandte Themen