library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.3.2
library(knitr)
## Warning: package 'knitr' was built under R version 4.3.3
df <- read.csv("C:/Users/ohcmo/Downloads/New/relig_rec.csv")

str(df)
## 'data.frame':    102 obs. of  9 variables:
##  $ category          : chr  "Religion & Spirituality" "Religion & Spirituality" "Religion & Spirituality" "Religion & Spirituality" ...
##  $ name              : chr  "relig_rec" "relig_rec" "relig_rec" "relig_rec" ...
##  $ label             : chr  "Religious preference" "Religious preference" "Religious preference" "Religious preference" ...
##  $ response          : chr  "Christian: FndPrtstnt\\,Orthdx\\,ChrOth" "Christian: FndPrtstnt\\,Orthdx\\,ChrOth" "Christian: FndPrtstnt\\,Orthdx\\,ChrOth" "Christian: FndPrtstnt\\,Orthdx\\,ChrOth" ...
##  $ breakdown         : chr  "Race" "Race" "Race" "Race" ...
##  $ breakdown_category: chr  "White" "White" "White" "White" ...
##  $ year              : int  1972 1973 1974 1975 1976 1977 1978 1980 1982 1983 ...
##  $ percent           : num  21.7 23.3 24.1 21.4 24.9 ...
##  $ std_err           : num  NA NA NA 1.5 1.6 1.86 1.92 1.84 1.64 2.01 ...
head(df)
##                  category      name                label
## 1 Religion & Spirituality relig_rec Religious preference
## 2 Religion & Spirituality relig_rec Religious preference
## 3 Religion & Spirituality relig_rec Religious preference
## 4 Religion & Spirituality relig_rec Religious preference
## 5 Religion & Spirituality relig_rec Religious preference
## 6 Religion & Spirituality relig_rec Religious preference
##                                  response breakdown breakdown_category year
## 1 Christian: FndPrtstnt\\,Orthdx\\,ChrOth      Race              White 1972
## 2 Christian: FndPrtstnt\\,Orthdx\\,ChrOth      Race              White 1973
## 3 Christian: FndPrtstnt\\,Orthdx\\,ChrOth      Race              White 1974
## 4 Christian: FndPrtstnt\\,Orthdx\\,ChrOth      Race              White 1975
## 5 Christian: FndPrtstnt\\,Orthdx\\,ChrOth      Race              White 1976
## 6 Christian: FndPrtstnt\\,Orthdx\\,ChrOth      Race              White 1977
##   percent std_err
## 1   21.67      NA
## 2   23.30      NA
## 3   24.08      NA
## 4   21.42    1.50
## 5   24.86    1.60
## 6   24.16    1.86
df_filtered <- df %>%
    filter(breakdown_category %in% c("White", "Black"))

variability_by_race <- df_filtered %>%
    group_by(breakdown_category) %>%
    summarise(
        mean_percent = mean(percent, na.rm = TRUE),
        sd_percent = sd(percent, na.rm = TRUE),
        cv_percent = sd_percent / mean_percent * 100
    )

overall_stats <- df_filtered %>%
    summarise(
        mean_percent = mean(percent, na.rm = TRUE),
        sd_percent = sd(percent, na.rm = TRUE),
        cv_percent = sd_percent / mean_percent * 100
    )

print(variability_by_race)
## # A tibble: 2 × 4
##   breakdown_category mean_percent sd_percent cv_percent
##   <chr>                     <dbl>      <dbl>      <dbl>
## 1 Black                      61.0       7.84       12.9
## 2 White                      26.7       3.31       12.4
print(overall_stats)
##   mean_percent sd_percent cv_percent
## 1     43.80132   18.28073   41.73558