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!
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)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With