R - Count duplicates values for each row

424 Views Asked by At

I'm working on a data frame that requires to calculate Fleiss's Kappa for inter-rater agreements. I'm using the 'irr' package for that.

Besides that, I need to count, for each observation, how many of raters are in agreement.

My data looks like these:

  a b c
1 1 1 1
2 1 2 2
3 2 3 2
4 3 3 1
5 4 2 1

I'm expecting something like this, , where count stands for number of raters on agreement

  a b c count
1 1 1 1 3 
2 1 2 2 2
3 2 3 2 2
4 3 3 1 2
5 4 2 1 0

Thanks a lot.

1

There are 1 best solutions below

0
On

Alternative solution if your data is in a data frame called abc:

as.numeric(apply(abc,1,function(x) { 
    ux<-unique(x); 
    tab <- tabulate(match(x, ux)); 
    mode <- ux[tab == max(tab)];
    ifelse(length(mode)==1,length(which(x==mode)),NA_character_);
} ))

When you run it gives:

[1]  3  2  2  2 NA