Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function to check if column names are unique

Tags:

dataframe

r

I'm working on a program and now I'm looking for a way to check the column names when uploading a file. If the names are not unique an error should be written. Is there any way to do this?

For example if I have these df:

> a <- c(10, 20, 30)
> b <- c(1, 2, 3)
> c <- c("Peter", "Ann", "Mike")
> test <- data.frame(a, b, c)

with:

library(dplyr)
test <- rename(test, Number = a)
test <- rename(test, Number = b)
> test
  Number Number     c
1     10      1 Peter
2     20      2   Ann
3     30      3  Mike

If this were a file how could I check if the column names are unique. Nice would be as result only True or False!

Thanks!

like image 863
Timothy_Goodman Avatar asked Mar 05 '23 01:03

Timothy_Goodman


1 Answers

We can use:

any(duplicated(names(df))) #tested with df as iris
[1] FALSE

On OP's data:

any(duplicated(names(test)))
[1] TRUE

The above can be simplified using the following as suggested by @sindri_baldur and @akrun

anyDuplicated(names(test))

If you wish to know how many are duplicated:

length(which(duplicated(names(test))==TRUE))
[1] 1

This can also be simplified to(as suggested by @sindri_baldur:

sum(duplicated(names(test)))
like image 111
NelsonGon Avatar answered Mar 11 '23 03:03

NelsonGon