0

I need to count the number of female in each department, instead of repeating the same kind of code for each department, I want to create a loop instead: old method:

by repeating the code

new attempt:

new attempt

Error I got:

enter image description here

I am wondering where went wrong inside the for loop. Thanks for your help.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Rosie612
  • 1
  • 2
  • 1
    Please don't post data or code as images. Take a look at [How to make a great reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and [How to ask](https://stackoverflow.com/help/how-to-ask) to get hints for improving your question. – Martin Gal Jul 17 '20 at 16:45
  • Welcome to SO Rosie. One good rule of thumb when wrking in R is "If I'm using a `for` loop, there's probably a better way of doing it". There are exceptions, but this isn't one of them. Look at grouping and summarising functions, either in base R or (for example) `dplyr`. You'll end up with shorter, more robust code that is easier to understand and maintain, and which runs more quickly. – Limey Jul 17 '20 at 16:56
  • In a nutshell, my question is instead of using: ```women1<- length(which(data$gender=="female" & data$department =="department1")) ``` and repeat multiple times for each department , how to use loop to improve efficiency? i have tried to create a list: ```department<-c("department1","department2"... "department6"), for (i in department) { women[i] <-length(which(data$gender=="female" & data$department =="[[i]]"]) print(women[i]) }``` but it gave me error. – Rosie612 Jul 17 '20 at 17:01
  • @ Limey, if for loop is not the best option to use in this situation, what method would be a more efficient one to use? I didn't choose dplyr because i only needed to look at a subset of departments not each department, and the results will be part of next calculation as well. dplyr in this case couldn't solve my problem. – Rosie612 Jul 17 '20 at 17:08

1 Answers1

0

If you just want the counts, use xtabs. This gives you rows for each gender:

xtabs(~Gender+'Business Segment', eelist06_30_20)
xtabs(~Gender+'Company', eelist06_30_20)

If you just want Female:

xtabs(~'Business Segment', eelist06_30_20, subset=Gender=="Female")

?xtabs for more details.

dcarlson
  • 10,936
  • 2
  • 15
  • 18
  • Thanks for the info but the thing I have been struggling with is how to create a loop on what i am counting here. I knew there are multiple ways to do conditional count in R such as ```nrow```, ```length``` and ```xtabs```. My intention is to create a list for all the departments i want to look at the count of female and then ideally the loop will yield a list of numbers for each department, and then use that list of number to do other calculations. – Rosie612 Jul 18 '20 at 14:41
  • My suggestion gives you that, just save the result by setting `women <- xtabs(...)` and you have a vector or a matrix with all of the totals that you can use for further purposes. If you provide reproducible data, I can provide more details. – dcarlson Jul 18 '20 at 23:45
  • Understood, I liked the tabular format xtabs yields, but two xtabs objects can't divide each other, can they? Eventually I need to get the count of female and the count of total employees and then the female ratio. – Rosie612 Jul 20 '20 at 14:07
  • You can extract (`?Extract`) individual cells from any table/matrix and use them in any way you want. If the matrices are the same dimension, you can easily divide them: `a <- matrix(1:9, 3, 3)`; `b <- matrix(9:1, 3, 3)`; `a/b`. – dcarlson Jul 20 '20 at 15:54
  • thanks for the hint! I actually converted the xtabs objects to matrices by using ```as.matrix ``` and the division worked.:) – Rosie612 Jul 20 '20 at 21:51