Q:

Employee turnover with R using multiple selected variables

I am having a few issues trying to calculate employee turnover rates using multiple variables.
See the sample data below.
As Of      Status       Gender    Type   Assignment
1/31/2015  Termination  Male      A       Fulltime
1/31/2015  Active       Female    A       Parttime
1/31/2015  Active       Male      B       Fulltime
1/31/2015  Active       Female    B       Parttime
1/31/2015  Active       Male      A       Fulltime
2/29/2015  Active       Female    A       Parttime
2/29/2015  Active       Male      B       Fulltime
2/29/2015  Active       Female    B       Parttime
2/29/2015  New Hire     Male      A       Fulltime
2/29/2015  Termination  Female    A       Parttime
3/31/2015  Active       Male      B       Fulltime
3/31/2015  Active       Female    B       Parttime
3/31/2015  Active       Male      A       Fulltime
3/31/2015  Termination  Female    A       Parttime
3/31/2015  Active       Male      B       Fulltime

I reshaped my data to count the number of Active and Number of Terminations per month with the code below.
dataSet <- read.table(textConnection(txt), header=TRUE)
dataSet$As.Of <- as.Date(dataSet$As.Of, format="%m/%d/%y")
dataSet$As.Of.Month <- format(dataSet$As.Of, "%m")

dataSetAgg <- dcast(dataSet, As.Of.Month ~ Status, fun.aggregate = length, value.var="As.Of.Month")

Using this I am able to transform the data set into something that looks like this.
As Of      Active       Terminations    New Hires
1/2015       4                1             0
2/2015       3                1             1
3/2015       4                1             0

And I am able to calculate the overall running turnover by doing
Running_terminations <- sum(dataSetAgg$Termination)/nrow(dataSetAgg)
Running_headcount  <- sum(dataSetAgg$Active)/nrow(dataSetAgg)
Annual_turnover <- (Running_terminations/Running_headcount)*12

Below is a description of what I mean by Running_termination, Running_headcount, and Annual_turnover.
#Running_terminations <-

should be (Terminations in month 1) + (Terminations in month 2)... / # of months
#Running_headcount <-

should be (Headcount in month 1) + (Headcount in month 2)... / # of months
#Annual_turnover <-

(Running terminations / Running headcount) * 12
What I would like to do is calculate the Annual_turnover via specified variables
For example,
Male_turnover <-
Male_and_fulltime_turnover <-
Male_and_female_and_TypeA_and_parttime_turnover <-

etc....
Is there a relatively simple way to do this?
Many thanks in advance.

A:

You can use sum(condition) to count the lines where condition=TRUE:

Number of lines with Status='Termination':
sum(dataSet$Status=='Termination')
3

Number of lines with Status='Termination' and Gender='Male':
sum(dataSet$Status=='Termination' & dataSet$Gender=='Male')
1

You can also use length() to count the number of items:

Number of (unique) month
length(unique(dataSet$As.Of.Month))
3

