vladimir84
vladimir84

Reputation: 23

How to extract unique rows from a data frame with an index column?

I'm importing from Excel to R, not duplicates and a row number of Excel (Index). If I do the unique() before setting up Index, the position of the rows in the data frame won't correspond to the Excel file. If I do unique() after Index, it will take into account also the Index column, and there won't be any duplicates.

This:

Index   a   b   c
1       12  12  14
2       12  12  14
3       11  12  13 

to this:

Index   a   b   c
1       12  12  14
3       11  12  13 

Code:

library(openxlsx)
library(tidyverse)
dati <- data.table(read.xlsx("\\\\192.168.x.x\\file.xlsx", detectDates = TRUE))
#Index row
dati <- tibble::rowid_to_column(dati, "Index")

(If it's a repeated question I apologize, I searched high and low for days and couldn't find anything. I have a feeling it's a very simple solution under a different keyword)

Upvotes: 0

Views: 196

Answers (1)

jay.sf
jay.sf

Reputation: 72813

You could use duplicated().

> df1[-which(duplicated(df1[,-1])), ]
  Index  a  b  c
1     1 12 12 14
3     3 11 12 13

Data

df1 <- structure(list(Index = 1:3, a = c(12L, 12L, 11L), b = c(12L, 
                                                               12L, 12L), c = c(14L, 14L, 13L)), class = "data.frame", row.names = c(NA, 
                                                                                                                                     -3L))

Upvotes: 2

Related Questions