##############################################
######      R-Commands - Semantics UAM    ####
######             Poznan 2021            ####
##############################################

# Step 1 - Copy the two columns from MS Excel / Google Sheets into “bloc notes” under Win(not Word Pad) or into BBEdit under Mac(not TextEdit). Save the file.

# Step 2 - Load data into R from the .txt file

df <- read.table(file.choose(), header=T, sep="\t")

# Step 3 - Check your data looks ok
summary(df, stringsAsFactors = T)

# Step 4 - Create a Cross-Tabulation of your data
xtab = table(df)

# Step 5 - Check your xtab looks ok
xtab

######################
###  SIGNIFICANCE  ###
######################

######################
#  Chi-Squared Test  #
######################

# This is the most common test in corpus linguistics / sociology
# It tests to see if the variation / differences could be chance.
# It gives you a p-value

CHI = chisq.test(xtab)
CHI

# Pearson residuals
# These show which cells are responsible for differences between the sets
RES= CHI$res
RES