#### --- Preparing the R Session --- ####
# Clearing out any remaining objects from previous sessions
rm(list=ls())
# Checking working directory
getwd()
# Setting working directory
setwd("C:/Users/lsmun/Downloads/Archival Correlational Study")
# Replace the part in quotation marks with your own file path
# Alternate "point-and-click" method:
# 1. Save this script file in the same folder on your computer where
you've saved your data
# (i.e., If your data is in "Downloads," save this file in your
"Downloads" too.)
# 2. Click "Session" in the R menu
# 3. Select "Set Working Directory"
# 4. Select "To source file location"
#### --- Loading in your data --- ####
# Pick the one that matches your file type.
# Comment out or delete the irrelevant ones.
# For a .csv file
dat <- read.csv("Project 1 Data - Merged Data.csv",# Replace the
asterisks (***) with your file name (leave the .csv at the end!)
na.strings="NA", # Replace with how NAs are coded
strip.white=TRUE)
#### --- Examining the structure of your dataset --- ####
# Get names, data types, and first 10 data points of every column
str(dat)
# Get a list of column names of your data (in order)
colnames(dat)
# Show the first five rows of your data.
head(dat)
# Show the whole dataset in the data viewer
View(dat)
# Summary + Histograms of full dataframe (Doesn't always work)
library(summarytools)
view(dfSummary(dat))
#### --- Analyzing Data --- ####
#### -- Demographics and Descriptives -- ####
dat$N <- as.numeric(gsub(",","",dat$N))
# Get the mean and sd for ordinal, interval, or ratio variables
mean(dat$N, na.rm = T) # sd
sd(dat$N, na.rm = T) # sd
mean(dat$Conservative, na.rm = T) # sd
sd(dat$Conservative, na.rm = T) # sd
mean(dat$Divorce.Rates, na.rm = T) # sd
sd(dat$Divorce.Rates, na.rm = T) # sd
#### -- Correlations -- ####
# Run a simple correlation between two variables
with(dat, cor.test(dat$Conservative, dat$Divorce.Rates,
alternative="two.sided", method="pearson"))
# "var1" is the column name of variable 1
# "var2" is the column name of variable 2