Reputation: 11
This is my data. For each year I want to get the proportion of each of marital_status. For instance, in the year 2000 proportion of married people is 57291/(57291+58238+18181). Like this, for each year and for each marital_case I want a proportion. But in R, when I am doing proportion, all the counts of marital_status are added and it gives a proportion of the whole data frame. I have tried group_by but doesn't work.
Upvotes: 0
Views: 27
Reputation: 177
Next time try and make a reprex (i.e. minimal reproducible example).
A minimal data table example here:
year <- c(2000, 2000, 2000, 2010, 2010, 2010)
marital_status <- c("married", "never-married", "sep-div-wid", "married", "never-married", "sep-div-wid")
n <- c(1254, 1000, 550, 1532, 1258, 450)
dataframe <- data.frame(year, marital_status, n)
To calculate proportion per year simply group_by()
year:
dataframe %>%
group_by(year) %>%
mutate(prop = n / sum(n))
Upvotes: 1