Lisann
Lisann

Reputation: 5855

Remove rows in dataframe based on three columns

I have a dataframe like z:

z <- matrix(c(1,0,0,1,1,0,0, 
      1,0,0,0,1,0,0, 
      0,0,0,0,0,0,0, 
      0,0,1,0,0,0,0), 
    nrow=7, 
    dimnames=list(LETTERS[1:7],NULL)) 

   [,1] [,2] [,3] [,4]
A    1    1    0    0
B    0    0    0    0
C    0    0    0    1
D    1    0    0    0
E    1    1    0    0
F    0    0    0    0
G    0    0    0    0

Now I want to remove the duplicated rows where the values of column 1, 2, and 3 are the same.

The result should be like this:

   [,1] [,2] [,3] [,4]
A    1    1    0    0
B    0    0    0    0
D    1    0    0    0

Could anyone help me with this? Many thanks!

Upvotes: 6

Views: 642

Answers (1)

Max
Max

Reputation: 4932

> z[rownames(unique(z[,-4])),]
  [,1] [,2] [,3] [,4]
  A    1    1    0    0
  B    0    0    0    0
  D    1    0    0    0

Upvotes: 7

Related Questions