Count number of rows matching a criteria

后端 未结 8 874
渐次进展
渐次进展 2020-11-29 05:01

I am looking for a command in R which is equivalent of this SQL statement. I want this to be a very simple basic solution without using complex functions OR dplyr type of pa

8条回答
  •  失恋的感觉
    2020-11-29 05:31

    mydata$sCode == "CA" will return a boolean array, with a TRUE value everywhere that the condition is met. To illustrate:

    > mydata = data.frame(sCode = c("CA", "CA", "AC"))
    > mydata$sCode == "CA"
    [1]  TRUE  TRUE FALSE
    

    There are a couple of ways to deal with this:

    1. sum(mydata$sCode == "CA"), as suggested in the comments; because TRUE is interpreted as 1 and FALSE as 0, this should return the numer of TRUE values in your vector.

    2. length(which(mydata$sCode == "CA")); the which() function returns a vector of the indices where the condition is met, the length of which is the count of "CA".

    Edit to expand upon what's happening in #2:

    > which(mydata$sCode == "CA")
    [1] 1 2
    

    which() returns a vector identify each column where the condition is met (in this case, columns 1 and 2 of the dataframe). The length() of this vector is the number of occurences.

提交回复
热议问题