How to count the number of observations in R like Stata command count

前端 未结 2 1868
旧时难觅i
旧时难觅i 2021-02-05 23:46
aaa<- data.frame(sex=c(1,1,2,2,1,1), group1=c(1,2,1,2,2,2),group2=c(\"A\",\"B\",\"A\",\"B\",\"A\",\"B\"))

stata command:

count if se         


        
2条回答
  •  臣服心动
    2021-02-06 00:33

    You can also use the filter function from the dplyr package which returns rows with matching conditions.

    > library(dplyr)
    
    > nrow(filter(aaa, sex == 1 & group1 == 2))
    [1] 3
    > nrow(filter(aaa, sex == 1 & group2 == "A"))
    [1] 2
    

提交回复
热议问题