0% found this document useful (0 votes)
730 views18 pages

Intro To R

This document provides an introduction and overview of basic commands in R for importing data, descriptive statistics, and bivariate analyses. It outlines how to import a CSV file, use comments, assign objects, and call help files. Descriptive statistics commands like summary(), describe(), mean(), and table() are demonstrated. Bivariate tests covered include t-tests, ANOVA, chi-square tests, and correlations. Missing data handling and converting variables to factors are also discussed.

Uploaded by

api-522167549
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
730 views18 pages

Intro To R

This document provides an introduction and overview of basic commands in R for importing data, descriptive statistics, and bivariate analyses. It outlines how to import a CSV file, use comments, assign objects, and call help files. Descriptive statistics commands like summary(), describe(), mean(), and table() are demonstrated. Bivariate tests covered include t-tests, ANOVA, chi-square tests, and correlations. Missing data handling and converting variables to factors are also discussed.

Uploaded by

api-522167549
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Introduction to R

For questions, contact:


Dr. Michael Bixter - [email protected]

Psych 739: Multivariate Statistics


Overview of Slides

• Basic R commands
• Bivariate statistics in R
Basic R Commands
Importing a csv file
Command:
read.csv(path name)

• Point-and-click method
 In RStudio, use the Import Dataset  From text (base)
option
Some useful functions and commands
• # (comment)
 In a line of code, anything after a # sign is a comment and R ignores it

• <- (assign)
 Assign what is whatever right of the arrow to the object on the left of
the arrow
 Example: x <- 2

• help() or ?
 Example: help(mean) or ?mean

• $ (subset a column in a data set)


 Example: survey$age
Some useful functions and commands
• c() (concatenate or combine function)
 Example: newData <- c(4, 2, 5, 6, 1)

• install.packages(“package name”, dep=TRUE)


 Example: install.packages(“psych”, dep=TRUE)

• library(package name)
 Loads an installed package to a new R session/environment
 Example: library(psych)
Descriptive Statistics
Metric data
• R function: summary(variable name)
• Example: summary(survey$age)
• Output

• describe() function in the psych package


Descriptive Statistics
Metric data
• Can use specific built-in descriptive functions
• R functions: mean(variable name), sd(variable name)
• Example: mean(survey$age), sd(survey$age)

BEWARE OF MISSING DATA!


mean(survey$smokenum)

mean(survey$smokenum, na.rm=TRUE)
Descriptive Statistics
Categorical data
• R function: table(variable name)
• Example: table(survey$child)
• Output
Bivariate Statistics
Independent samples t-test
R command: t.test(DV ~ IV)
• Example 1: t.test(survey$Mlifesat ~ survey$sex)
• Example 2: t.test(Mlifesat ~ sex, data = survey)
Equal Variances Assumed
R command: t.test(DV ~ IV, var.equal = TRUE)
• Example 1: t.test(survey$Mlifesat ~ survey$sex, var.equal = TRUE)
• Example 2: t.test(Mlifesat ~ sex, data = survey, var.equal = TRUE)
One-way ANOVA
R command: aov(DV ~ IV)
• BEWARE: IV must be a factor type in R
 You can check using: class(variable name)
• If not, convert IV to a factor:
 Example: survey$age_group <- as.factor(survey$age_group)

• Example 1: aov(survey$Mnegaff ~ survey$age_group)


• Example 2: aov(Mnegaff ~ age_group, data = survey)
One-way ANOVA
• Save the aov command to an object
 Example: anovaTest <- aov(Mnegaff ~ age_group, data = survey)
 Then you can use the summary(object name) function to get the output
Additional ANOVA commands
Tukey’s HSD post-hoc: TukeyHSD(object name)
• Example: TukeyHSD(anovaTest)
Additional ANOVA commands
Levene’s Test
• In the car package
• Command: leveneTest(object name)
Chi-square test
First create a contingency table with your two categorical
variables
• Example: tbl <- table(survey$age_group, survey$child)

Then use the R function: chisq.test(table name)


• Example: chisq.test(tbl)
Pearson’s correlation
R command: cor(Var1, Var2)
• Example: cor(survey$Mposaff ~ survey$Mnegaff)

• BEWARE OF MISSING VALUES

You might also like