Home > OS >  Verifyin if there's at least two columns have the same value in a specefic column
Verifyin if there's at least two columns have the same value in a specefic column

Time:10-28

i have a data and i want to see if my variables they all have unique value in specefic row let's say i want to analyze row D

my data

Name      F     S     T
A         1     2     3
B         2     3     4
C         3     4     5
D         4     5     6


> TRUE (because all the three variables have unique value)

Second example

Name      F     S     T
A         1     2     3
B         2     3     4
C         3     4     5
D         4     5     4

>False (because F and T have the same value in row D )

CodePudding user response:

You can use dplyr for this:

df %>%
  summarize_at(c(2:ncol(.)), n_distinct) %>%
  summarize(if_all(.fns = ~ .x == nrow(df)))

CodePudding user response:

In base R do

f1 <- function(dat, ind) {
    
    tmp <- unlist(dat[ind, -1])
    length(unique(tmp)) == length(tmp)
}

-testing

> f1(df, 4)
[1] TRUE
> f1(df1, 4)
[1] FALSE

data

df <- structure(list(Name = c("A", "B", "C", "D"), F = 1:4, S = 2:5, 
    T = 3:6), class = "data.frame", row.names = c(NA, -4L))
df1 <- structure(list(Name = c("A", "B", "C", "D"), F = 1:4, S = 2:5, 
    T = c(3L, 4L, 5L, 4L)), class = "data.frame", row.names = c(NA, 
-4L))
  • Related