Count number of rows matching a criteria

后端 未结 8 883
渐次进展
渐次进展 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:37

    1. mydata$sCode is a vector, it's why nrow output is NULL.
    2. mydata[mydata$sCode == 'CA',] returns data.frame where sCode == 'CA'. sCode includes character. That's why sum gives you the error.
    3. subset(mydata, sCode='CA', select=c(sCode)), you should use sCode=='CA' instead sCode='CA'. Then subset returns you vector where sCode equals CA, so you should use

      length(subset(na.omit(mydata), sCode='CA', select=c(sCode)))

    Or you can try this: sum(na.omit(mydata$sCode) == "CA")

提交回复
热议问题