how to realize countifs function (excel) in R -
i have dataset containing 100000 rows of data. tried countif
operations in excel, prohibitively slow. wondering if kind of operation can done in r? basically, want count based on multiple conditions. example, can count on both occupation , sex
row sex occupation 1 m student 2 f analyst 2 m analyst
easy peasy. data frame this:
df <- data.frame(sex=c('m','f','m'), occupation=c('student','analyst','analyst'))
you can equivalent of countif
first specifying if
part, so:
df$sex == 'm'
this give boolean vector, i.e. vector of true
, false
. want count observations condition true
. since in r true
, false
double 1 , 0 can sum()
on boolean vector. equivalent of countif(sex='m')
therefore
sum(df$sex == 'm')
should there rows in sex
not specified above give na
. in case, if want ignore missing observations use
sum(df$sex == 'm', na.rm=true)
Comments
Post a Comment