is there any command in R language with the same operation with Countifs() formula in excel?
CodePudding user response:
In R, if you have a vector:
vector <- c(rep(4,3), rep(3,3))
you can say, for example:
vector == 4
which will give you the following output:
[1] TRUE TRUE TRUE FALSE FALSE FALSE
so, sinces TRUE values equals 1 under the hood, then you can say
sum(vector == 4)
output:
[1] 3
which is like countif values of vector are equals to 4
CodePudding user response:
Base R:
# If you want the total count:
ave(df$group_vector, df$group_vector, FUN = length)
# If you want a sequential count as in `COUNTIFS($A$1:$A1, $A1)`
ave(df$group_vector, df$group_vector, FUN = seq.int)