Back to RStudio and Project Skills

Higher Applications of Mathematics

RStudio reference

Quick reference cards for common RStudio commands, when to use them, and how to interpret the output.

Importing a CSV file

data <- read.csv("file.csv")

Use when

Use when loading a spreadsheet-style data file into RStudio.

Output means

Creates a data frame called data.

Example

survey <- read.csv("survey.csv")

Checking column names

names(data)

Use when

Use after importing data to check exact variable names.

Output means

Lists the column names you can use in commands.

Example

names(survey)

Summary statistics

mean(x), median(x), sd(x), summary(x)

Use when

Use to describe centre, spread and range.

Output means

Gives numerical summaries for a variable.

Example

mean(survey$height)

Box plots

boxplot(x)

Use when

Use to compare spread, median and possible outliers.

Output means

Shows median, quartiles, range and outliers.

Example

boxplot(survey$travel_time)

Histograms

hist(x)

Use when

Use to show the shape of a numerical distribution.

Output means

Shows frequencies in grouped intervals.

Example

hist(survey$screen_time)

Scatter plots

plot(x, y)

Use when

Use to show the relationship between two numerical variables.

Output means

Each point represents one pair of values.

Example

plot(survey$revision, survey$score)

Correlation

cor(x, y)

Use when

Use to measure strength and direction of a linear relationship.

Output means

Returns a value between -1 and 1.

Example

cor(survey$revision, survey$score)

Linear regression

model <- lm(y ~ x, data = data)

Use when

Use to fit a straight-line model and make predictions.

Output means

Gives intercept, gradient and model summary.

Example

lm(score ~ revision, data = survey)

Confidence intervals

t.test(x)$conf.int

Use when

Use to estimate a population mean from sample data.

Output means

Gives a plausible interval for the true mean.

Example

t.test(survey$height)$conf.int

One-sample t-test

t.test(x, mu = value)

Use when

Use to test whether a sample mean differs from a claimed mean.

Output means

Use the p-value to judge evidence against the claim.

Example

t.test(times, mu = 30)

Paired t-test

t.test(before, after, paired = TRUE)

Use when

Use when the same people are measured twice.

Output means

Tests whether the mean paired difference is 0.

Example

t.test(before, after, paired = TRUE)

Two-proportion test

prop.test(c(success1, success2), c(total1, total2))

Use when

Use to compare two sample proportions.

Output means

Gives a p-value for evidence of a difference in proportions.

Example

prop.test(c(42, 55), c(100, 120))

Exporting or saving results

write.csv(results, "results.csv")

Use when

Use when saving a table for a project write-up.

Output means

Creates a CSV file from an R object.

Example

write.csv(summary_table, "summary.csv")

Common RStudio errors

Check spelling, capitals, brackets and data names

Use when

Use when R says an object is not found or a command fails.

Output means

Most early errors come from mistyped names or missing brackets.

Example

survey$Height is different from survey$height.