Count the number of rows where all columns have identical values

I have a dataframe and I want to count the number of rows which have the same value for all the columns, within each row.

For example, I have this data:

cmp <- read.table(text = "
A B C D
1 1 1 0
1 1 1 1
2 2 2 2
3 3 3 0", header = TRUE)

Here, the count is 2, because the second row and third row have only one unique value each, only 1s, and only 2s, respectively.

Thanks in advance.

Upvotes: 4

Views: 1825

Answers (4)

RoyalTS
RoyalTS

Reputation: 10203

The tidyverse way:

df %>% 
  rowwise() %>% 
  mutate(unique_vals = length(unique(c_across(everything()))))

This gives you the number of unique values for the selected columns -- feel free to change everything() to whatever you need. You can then filter/sum this variable as you please.

Upvotes: 2

d.b
d.b

Reputation: 32538

You could check if maximum value and minimum value across the rows are same

sum(do.call(pmax, cmp) == do.call(pmin, cmp))
#[1] 2

To obtain the rows where identical values are present

which(do.call(pmax, cmp) == do.call(pmin, cmp))
#[1] 2 3

Upvotes: 3

Henrik
Henrik

Reputation: 67778

Count the number of values per row which are equal to the first value. If this count is equal to the number of columns, then all values in the row are identical.

sum(rowSums(cmp == cmp[ , 1]) == ncol(cmp))
#[1] 2

Upvotes: 4

Josh O&#39;Brien
Josh O&#39;Brien

Reputation: 162311

This, which uses apply() to count the number of distinct elements in each row, should do the trick:

sum(apply(cmp, 1, function(x) length(unique(x))==1))
## [1] 2

Upvotes: 7

Related Questions