how to realize countifs function (excel) in R

前端 未结 5 942
面向向阳花
面向向阳花 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 09:46

    Given a dataset

    df <- data.frame( sex = c('M', 'M', 'F', 'F', 'M'), 
                      occupation = c('analyst', 'dentist', 'dentist', 'analyst', 'cook') )
    

    you can subset rows

    df[df$sex == 'M',] # To get all males
    df[df$occupation == 'analyst',] # All analysts
    

    etc.

    If you want to get number of rows, just call the function nrow such as

    nrow(df[df$sex == 'M',])
    

提交回复
热议问题