Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out if column in R table includes duplicate values?

Tags:

r

duplicates

I've got a lovely dataframe, my very first, and I'm starting to get the hang of R. One thing I haven't been able to find is a test for duplicate values. I have one column that I'm pretty sure is all unique values, but I don't know that.

Is there a way I can ask? For simplicity, let's pretend this is my data:

  var1 var2 var3
1    1    A    1
2    2    B    3
3    3    C   NA
4    4    D   NA
5    5    E    4

and I want to know whether var1 ever repeats.

like image 815
Amanda Avatar asked Nov 27 '12 22:11

Amanda


People also ask

How do you check if a column has repeated values?

Using the GROUP BY clause to group all rows by the target column(s) – i.e. the column(s) you want to check for duplicate values on. Using the COUNT function in the HAVING clause to check if any of the groups have more than 1 entry; those would be the duplicate values.

How do you filter out duplicates in a column in R?

distinct() function can be used to filter out the duplicate rows. We just have to pass our R object and the column name as an argument in the distinct() function.

How do I check if a column is unique in R?

To find unique values in a column in a data frame, use the unique() function in R. In Exploratory Data Analysis, the unique() function is crucial since it detects and eliminates duplicate values in the data.


1 Answers

Check out the duplicated function:

duplicated(dat$var1) # the rows of dat var1 duplicated

Documentation is here.

You should also look at the unique function.

like image 55
Erik Shilts Avatar answered Oct 21 '22 11:10

Erik Shilts