#######################################
######       R-Commands - Basics   ####
######        Paris 2020-2021      ####
#######################################

##########################
### Loading libraries ####
##########################

# Modules (packages or libraries) must be downloaded and installed
# Before a package can be used, it must be "started up"
# the command to start up a package is library
# Examples of libraries include:

MASS
rms
pvclust
ca
explor
FactoMineR

#################
####  DATA   ####
#################

####################
### Loading data ###
####################

# Step one - Copy data from Excel into “bloc note” (not Word Pad)
# Step two - Load data into R from txt file

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

# If the data are in the form of a numerical table, you must add row.names= 1
# Loading contingency table from a text file (copied from Excel)

xtab <- read.table(file.choose(), header=T, sep="\t", row.names= 1)

#######################
### Looking at data ###
#######################

# Summary of dataframe
summary(df, stringsAsFactors = T)

# First rows of data
head(df)

# Summary of one 'column' of a dataframe
table(df$NameOfColumn)

# Summary of contingency table
xtab

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

# Tests to see if difference between two sets of numbers is chance

# load the data from an cross-tabulation created in excel
# copy cross-tabulation to text file and load into R
xtab <- read.table(file.choose(), header=T, sep="\t", row.names = 1)

# check data, to make sure it has worked
xtab

# OR
# load the two variables from a data frame
# copy columns to a text file and load into %
df <- read.table(file.choose(), header=T, sep=“\t”)

# convert to cross-tabulation
xtab <- table(df)

# check data, to make sure it has worked
xtab

######################
#  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

# Observed vs. Expected outcomes
O = CHI$observed
E = CHI$expected
E
O
(O-E)^2/E

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

#####################
### Handling data ###
#####################

# Change the order of features in a variable 
# (important in Logistic Regression)
df $ ColumnName = relevel(df$ColumnName, ref= "FeatureName")

# Remove Columns
df $ NameOfColumn=NULL

# convert the data to cross-tabulation in R 
# make sure you have run the script at the end of this file
xtab = stack.fnc(df)

# check data, to make sure it has worked
xtab

# Creating a contingency table from
# For Cluster Analysis and Binary Correspondence Analysis

xtab = stack.fnc(df)

# Turn contingency table around (transpose)
xtab.t = t(xtab)
xtab.t

# Telling R that a given variable is a factor 
# (non numeric variable, often important for dates)
df $ ColumnName = as.factor(df$ColumnName)

# Telling R that a given variable is ordinal, not nominal 
# (essential for ordinal regression)
df $ ColumnName = as.ordered(df$ColumnName)

# Telling R that a given variable is numeric not categorical 
# (not sure why you would need this :)
df $ ColumnName = as.numeric(df$ColumnName)

# Telling R not to ignore NA
df $ ColumnName = addNA(df$ColumnName)

#######################################################
### Converting flat data frame to contingency table ###
#######################################################

# Step 1: Load the data frame
# Important! Make sure the first column in the data frame 
# (in excel) is the column you want as row names 

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

# Step 2: Run this script 
# Copy / paste into R and enter. Only do once per session.

stack.fnc=function(df.flat){
 for(i in 1:ncol(df.flat))
 df.flat[,i]=factor(df.flat[,i])
 df.stacked.nrow=length(levels(df.flat[,1]))
 df.stacked.ncol=0
 for(i in 2:ncol(df.flat))
 df.stacked.ncol=df.stacked.ncol+length(levels(df.flat[,i]))
 k=1
 df.stacked.colnames=rep("X",df.stacked.ncol)
  for(i in 2:ncol(df.flat))
  for(j in 1:length(levels(df.flat[,i]))) {df.stacked.colnames[k]=paste(colnames(df.flat[i]),levels(df.flat[,i])[j],sep=".")
  k=k+1}
df.stacked=data.frame(matrix(nrow=df.stacked.nrow,ncol=df.stacked.ncol))
df.stacked=matrix(data=rep(0,df.stacked.nrow*df.stacked.ncol),nrow=df.stacked.nrow,ncol=df.stacked.ncol,dimnames=list(levels(df.flat[,1]),df.stacked.colnames))
  for(i in 2:ncol(df.flat))
  for(j in 1:nrow(df.flat))
  df.stacked[as.character(df.flat[j,1]),paste(colnames(df.flat)[i],df.flat[j,i], sep=".")]=
  df.stacked[as.character(df.flat[j,1]),paste(colnames(df.flat)[i],df.flat[j,i], sep=".")]+1
  rm(list=c("df.stacked.ncol","df.stacked.nrow","i","j","k"))
  return(data.frame(df.stacked))
  }

# Step 3: This creates your contingency table called "xtab"
xtab = stack.fnc(df)
# Step 4: Check it has worked
xtab