Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for identical values across multiple columns in a dataframe [closed]

Tags:

r

I have a dataframe with 15 factor columns. I need to create a new column that gives a true/false binary for whether all the values in a given row (for just those columns) are identical.

I've found this strategy...

identical(df[['A']],df[['B']])

...but that checks the entire column, whereas I need a solution by row.

like image 836
Kasey Avatar asked Feb 06 '26 04:02

Kasey


1 Answers

Using DF as shown in order to test it, apply unique to each row and for each row check whether the length of the result is 1

DF <- data.frame(A = 1:3, B = c(1:2, 1), C = 1)
transform(DF, same = apply(DF, 1, function(x) length(unique(x)) == 1))

giving:

  A B C  same
1 1 1 1  TRUE
2 2 2 1 FALSE
3 3 1 1 FALSE

Alternately using dlyr 1.0 or later:

library(dplyr)
DF %>%
 rowwise %>%
 mutate(same = n_distinct(unlist(cur_data())) == 1) %>%
 ungroup
like image 116
G. Grothendieck Avatar answered Feb 07 '26 17:02

G. Grothendieck