####Slide 6 - Commonly used oparators DS <- c(5,6,3,6) DS.1 <- c('A','B','C') #Both DS and DS.1 are now objects that contain the data to the right of the <- # This allows me to write comments in my syntax 2 > 1 #This will return a logical value -- True 2 == 2 (2 > 3 | 3 > 2) #This statement is asking R "Is 2 greater than 3 or is 3 greater than 2?" #If either one of those statements is true then R will return TRUE (2 > 3 | 3 < 2) #Because neither statement is true, R will return FALSE 2 + 2 #Addition 2 - 2 #Subtraction 2^2 #Exponentiation 2*2 #Multiplication ####Slide 8 - Formatting your data for R: Three easy steps Dataset <- read.csv('Intro R Data.csv') #This reads in your dataset read.table() will read in text files Dataset #This will return your entire dataset ####Slide 9 - Formatting your data for R: Common Mistakes Dataset <- read.csv('C:\Intro R Data.csv') #See R won't read \ Dataset <- read.csv('C:\\intro to R Data.csv') #To R case matters. This is a pain Dataset <- read.csv('C:\\Intro R Data.txt') #Can't locate your data because it's a csv file not txt file ####Slide 10 - Working with data in R: Things to check dim(Dataset) #This returns the dimensions of your dataframe; rows x columns names(Dataset) #This returns the column (variable) names of your dataset colMeans(Dataset[,1:10], na.rm=T) #This returns the means for columns 1 to 10 sapply(Dataset[,1:10], na.rm=T) #This returns the standard deviations of columns 1 to 10 ####Slide 11 - Working with data in R: Subsetting your dataset Dataset[5,1] #Returns the observation that resides in row 5 column 1 Dataset[,1] #Returns every observation that resides in column 1 Dataset[2, 1:5] #Returns every observation that resides in row 2 columns 1 through 5 ####Slide 12 - Working with Data in R: Subsetting your data Dataset$Var1 #This does the same thing that Dataset[,1] does #Returns every observation that resides in column 1 ds.Female <- Dataset[Dataset$Var11 == 'Female',] #This will create an R dataframe named ds.Female that only contains observations if Var = Female ####Slide 13 - Working with data in R: Reverse Coding Dataset$Var12 <- 8 - Dataset$Var10 #Creates another column for Dataset named Var12 and sets it equal to 8 - Var10 #I used 8 because my hypothetical scale ranges from 1 to 7 -- if it ranged from 1 to 5 I would have used 6 instead of 8 cor(Dataset$Var10, Dataset$Var12, use='complete.obs') ####Slide 14 - Working with Data in R: Internal R Functions mean(Dataset$Var1, na.rm=T) #Provides the mean for Var1 sd(Dataset$Var1, na.rm=T) #Provides the sd for Var1 min(Dataset$Var5, na.rm=T) #Provides the minimum value in Var1 max(Dataset$Var5, na.rm=T) #Returns the maximum value in Var5 cor(Dataset, use='complete.obs') #Correlates every variable in your dataset ####Slide 15 - Working with data in R: Internal R Functions modlm <- lm(Var2 ~ Var3, data=Dataset) #OLS Regression -- Variable 2 onto variable 3 summary(modlm) #Provides the results of the OLS regression modanova <- lm(Var4 ~ as.factor(Var11), data=Dataset) #OLS Regression -- Var2 on gender; as.factor() tells R the variable is a factor #This is an ANOVA! summary(modanova) modanova1 <- aov(Var4 ~ as.factor(Var11), data=Dataset) #aov is R's built in ANOVA function -- look at similarities between modanova and modanova1 -- IDENTICAL! summary(modanova1) dif <- TukeyHSD(modanova1) #Tukey's Honestly Significant Difference Test; Compare it to the modanova B1 -- The same ####Slide 14 - Exporting data from R write.csv(Dataset, 'Exported Dataset.csv') #Writes your R data to a csv file write.table(Dataset, 'Exported Dataset.txt') #Writes your R data to a txt file