Advertisement
bdill

religous_nones_gss_data.R

Apr 18th, 2019
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.08 KB | None | 0 0
  1. #----- Religious Nones - GSS data 1972 - 2018
  2. # https://twitter.com/pjmccann3/status/1109127955907837952
  3. # https://t.co/3tBqjPBXeb  dta download url
  4. # D:\Downloads\gss_full.dta
  5. #rm(list=ls())
  6.  
  7. library(tidyverse)
  8.  
  9. d = read_dta("D:/Downloads/gss_full.dta")
  10.  
  11. saveRDS(d, "C:/Data/R/religous_nones_gss_data.rds")  # CYA save
  12. # FYI, the *.dta file was 445 MB while the *.rds file was a mere 33 MB.  R compression FTW!
  13.  
  14. #----- Get raw data of desired columns -----
  15. datraw <- select(d, year, evangelical, mainline, blackprot, catholic, jewish, otherfaith, nofaith)
  16.  
  17. #----- counts by year -----
  18. datcount <- d %>%
  19.   group_by(year) %>%
  20.   summarize(
  21.     total = sum(evangelical + mainline + blackprot + catholic + jewish + otherfaith + nofaith),
  22.     evangelical = sum(evangelical),
  23.     mainline = sum(mainline) ,
  24.     blackprot = sum(blackprot),
  25.     catholic = sum(catholic) ,
  26.     jewish = sum(jewish) ,
  27.     otherfaith = sum(otherfaith),
  28.     nofaith = sum(nofaith)
  29.   )
  30.  
  31. #----- percents by year -----
  32. datpercent <- d %>%
  33.   group_by(year) %>%
  34.   summarize(
  35.     total = sum(evangelical + mainline + blackprot + catholic + jewish + otherfaith + nofaith),        
  36.     evangelical = sum(evangelical) / total,
  37.             mainline = sum(mainline) / total,
  38.             blackprot = sum(blackprot) / total,
  39.             catholic = sum(catholic) / total,
  40.             jewish = sum(jewish) / total,
  41.             otherfaith = sum(otherfaith) / total,
  42.             nofaith = sum(nofaith) / total
  43.          )
  44.  
  45. #----- save off data as TSV files -----
  46. write.table(datraw, file = "C:/Data/R/religous_nones_gss_data_raw.tsv", sep = "\t", row.names = FALSE, quote = FALSE)
  47. write.table(datcount, file = "C:/Data/R/religous_nones_gss_data_count.tsv", sep = "\t", row.names = FALSE, quote = FALSE)
  48. write.table(datpercent, file = "C:/Data/R/religous_nones_gss_data_percent.tsv", sep = "\t", row.names = FALSE, quote = FALSE)
  49.  
  50. # tidy up the dataset for ggplot
  51. tidydat <- gather(dpercent, key="denomination", value="count",  -year, -total)
  52.  
  53. ggplot(tidydat, aes(x = year, y = count)) +
  54.   geom_line(aes(color=denomination))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement