-3

I am struggling with a problem in R. I want to count all the ages for a specific gender and then write their absolute and relative frequenceis.

Consider my data is:

AGE    Place    Gender
12     SA       M
33     MA       F
14     KA       N
36     MA       N
32     DA       N
36     HA       F

In the data above, I would like to sum all occurrences of AGE for M, F and N, so the result will be something like:

AGE    Gender
12      M
69      F
82      N

How can I do this in R?

Thank You.

Jopan
  • 3
  • 5

1 Answers1

0

You can also do this quite easily with basic R code How to sum a variable by group. Or the tidyverse approach

library(tidyverse)

tribble(~AGE,    ~Place,    ~Gender,
12,     "SA",       "M",
33,     "MA",       "F",
14,     "KA",       "N",
36,     "MA",       "N",
32,     "DA",       "N",
36,     "HA",       "F") %>% 
  group_by(Gender) %>% 
  summarise(AGE =sum(AGE))

The output:

# A tibble: 3 x 2
  Gender   AGE
* <chr>  <dbl>
1 F         69
2 M         12
3 N         82
Tom
  • 53
  • 6