user9605051
user9605051

Reputation: 61

Count number of rows that contain same value?

Let's say I have a data frame that contains two columns "a" and "b". I would like to count the number of rows that contain the same value for both columns for a given row.

E.g. If row 1 of column "a" contains "blue" If row 1 of column "b" contains "blue" than that will count as 1.

Upvotes: 0

Views: 105

Answers (1)

r2evans
r2evans

Reputation: 160952

Something as simple as sum(df$a == df$b) will give you the right answer, assuming there are no NA in the data.

If you want to be safe, you can use something like this:

`%==%` <- function(a,b) {
  ana <- is.na(a)
  bna <- is.na(b)
  (ana & bna) | (!ana & !bna & a==b)
}
x1 <- c(1,2,NA,4)
x2 <- c(2,2,NA,NA)
x1 %==% x2
# [1] FALSE  TRUE  TRUE FALSE
sum(x1 %==% x2)
# [1] 2

Upvotes: 2

Related Questions