Reputation: 525
I have a data frame with a variable called "In Arrears?" with the answer being Yes, No or Zero. The other variable is called Trade, which lists different trades e.g. Electrical, Plumbing etc. I want to count how many Electrical Trades are called when In arrears and when not ( I want to do this for each Trade, I think there are 18 Trades in total). I was struggling with aggregate so decided I would:
Split the data frame into 2, one with In Arrears? = Yes and one with In Arrears? = No or Zero.
How do I do this with character variables? I've done this before with numeric variables and it's worked fine but can't find much on info on character variables at all. I.E how to do "if variable = "yes"" sort of thing.
Upvotes: 0
Views: 43
Reputation: 1338
dat = data.frame(trade = sample(letters[1:5],30,replace=TRUE),
arrears = sample(c("yes","no","0"),30,replace=TRUE))
Use dplyr
's group_by
to aggregate and summarise
the data according to your conditions:
require(dplyr)
dat %>% group_by(trade) %>%
summarise(no = length(arrears[arrears%in%c("no","0")]),
yes = length(arrears[arrears=="yes"]))
Upvotes: 1