0

enter image description here

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.

r2evans
  • 141,215
  • 6
  • 77
  • 149
upoma
  • 11
  • 2
  • 2
    Ummm ... that's a _picture_ of your data, many (including myself) will not spend time transcribing your data into something useful. Please: (1) don't include *just* images of data, it is more helpful to have it as something we can use, please use `dput(.)`, `read.table(.)`, or (fine with _this_ data) perhaps just copy the output from printing it on the console; (2) please include the code you've tried, its results (including errors/warnings), and why it is not correct if not obvious. – r2evans Mar 18 '22 at 12:00
  • 1
    Please see https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info – r2evans Mar 18 '22 at 12:01
  • Does this answer your question? [Understanding dplyr and group\_by](https://stackoverflow.com/questions/64635017/understanding-dplyr-and-group-by) – Anthony Ebert Mar 18 '22 at 12:02

1 Answers1

1

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))
edv
  • 157
  • 8