-3

How to do countif in R

IN EXCEL we can write the formula as

"COUNTIF($K$2:$K$205,K2),COUNTIF($K$2:$K$205,K3),.... "

How to do in R

value   col Countif
 1       A  3
 1       A  3
 1       A  3
 4       A  2
 4       A  2
 3       A  1
 99      B  2
 99      B  2
1000     B  4
1000     B  4
1000     B  4
1000     B  4
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
snr123
  • 23
  • 2

1 Answers1

-1

We can use the convenient function from dplyr i.e. add_count

library(dplyr)
df1 %>%
     add_count(value, col)

which is similar to

df1 %>%
    group_by(value, col) %>%
    mutate(count = n())
akrun
  • 874,273
  • 37
  • 540
  • 662