Using mutate to find the percentage of a variable

Hi, I have a new problem. I was trying to use mutate to calculate the percentage of MARRIED respondents (of each year) over years as a new variable. Here is what I have.

ex2 <- group_by(gss,year)
mutate(gss, PercentOfMarried = "MARRIED"/marital)

I've got a result but the outputs were all NA. Can someone please help me? Part of the dataset is attached as a screenshot. Thank you.

Please see FAQ: What's a reproducible example (`reprex`) and how do I do one?. Screenshots are generally unhelpful.

See if you can apply this approach

mtcars %>% group_by(gear) %>% count() %>% mutate(pct_tot = n/nrow(mtcars)*100)
#> # A tibble: 3 x 3
#> # Groups:   gear [3]
#>    gear     n pct_tot
#>   <dbl> <int>   <dbl>
#> 1     3    15    46.9
#> 2     4    12    37.5
#> 3     5     5    15.6

Created on 2020-02-17 by the reprex package (v0.3.0)

This is not quite right... from the code, you are dividing a character by a character column. Do you see this ? The syntax is not correct for R code.

mutate(gss, PercentOfMarried = mean(marital == "MARRIED"))

may result in what you want. marital == "MARRIED" would be TRUE when the respondant answered MARRIED and the mean would be equivalent to your percentage I guess.

Hope it helps


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