0

i hope there is a simple solution for this and i'am just being silly. I would like to calculate the Total of the age's for each person if their Occupation is equal to a certain string value.

Below is an extract of what they data looks like:

  Occupation Education Age Died
1  household Secondary  39   no
2    farming   primary  83  yes
3    farming   primary  60  yes
4    farming   primary  73  yes
5    farming Secondary  51   no
6    farming iliterate  62  yes

Ultimately i'd like to find the average age for each occupation catergory but just struggling to find the sum of the ages for each occupation.

There are 10 unique catergories for Occupation

business/service 
farming                
household       
others/worker    
others/unknown   
professional             
retiree          
student               
unemployed
rsteward
  • 51
  • 4

1 Answers1

0

You can use the group_by() and summarize functions from dplyr to do this:

data %>% 
    group_by(Occupation) %>%
    summarize(total_age = sum(Age))

You can also get average age directly:

data %>% 
    group_by(Occupation) %>%
    summarize(avg_age = mean(Age))

sdg
  • 165
  • 8