how to realize countifs function (excel) in R

前端 未结 5 950
面向向阳花
面向向阳花 2020-11-30 08:58

I have a dataset containing 100000 rows of data. I tried to do some countif operations in Excel, but it was prohibitively slow. So I am wondering if this kind o

5条回答
  •  广开言路
    2020-11-30 10:04

    Table is the obvious choice, but it returns an object of class table which takes a few annoying steps to transform back into a data.frame So, if you're OK using dplyr, you use the command tally:

        library(dplyr)
        df = data.frame(sex=sample(c("M", "F"), 100000, replace=T), occupation=sample(c('Analyst', 'Student'), 100000, replace=T)
        df %>% group_by_all() %>% tally()
    
    
    # A tibble: 4 x 3
    # Groups:   sex [2]
      sex   occupation `n()`
             
    1 F     Analyst    25105
    2 F     Student    24933
    3 M     Analyst    24769
    4 M     Student    25193
    

提交回复
热议问题