How can find the total number of males and females based on a number of another column ?

#

I want to know how to sum total number of males and females. Thank you in advance!

Hi, first of all, please provide reproducible examples, not screenshots.

Secondly, it would be good if you shared your code. What have you tried?

Thirdly, have a look at dplyr::count().

1 Like

Hi,

Welcome to the RStudio community!

I agree with @williaml that you should have a look at how to build a reprex to better share your data. But given this is your first time posting here, and the problem is relatively easy, I was able to quickly create some fake data and show a way to solve it

library(dplyr)

set.seed(1)
myData = data.frame(
  Gender = sample(c("Male", "Female"), 5, replace = T),
  Number = sample(1:20, 5)
)
myData
#>   Gender Number
#> 1   Male     11
#> 2 Female     14
#> 3   Male     18
#> 4   Male      1
#> 5 Female      5

myData %>% group_by(Gender) %>% 
  summarise(total = sum(Number), .groups = "drop")
#> # A tibble: 2 x 2
#>   Gender total
#>   <chr>  <int>
#> 1 Female    19
#> 2 Male      30

Created on 2021-11-22 by the reprex package (v2.0.1)

Hope this helps,
PJ

2 Likes

Thank you a lot! This helped a lot and I also found another way of solving this problem:

sum(Datt[Datt$Gender == "Male", "Number"])
sum(Datt[Datt$Gender == "Female", "Number"])

Hi,

You're welcome.
Yes the solution that you supplied in the last comment is how you can do this in base R, so not using the Tidyverse.

If you like to learn more about the Tidyverse (dplyr package in this case):

PJ

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.