Getting started with R

What R is, why researchers use it, and your first working session: vectors, data frames, and the built-in datasets you will practice on. Runnable in your browser.

About 12 minutes, every example runs in your browser

R is a free programming language built for statistics. It is what many statisticians, epidemiologists, and psychologists actually use to analyse study data, and it is one of the two engines KyroStat runs behind the scenes. Learning even a little R pays off twice: you understand what your statistics software is really doing, and you can read the analysis code that journals and supervisors increasingly ask for.

This course uses only base R: no packages to install, no setup. Every grey code panel has a "Run it yourself" link that opens our in-browser playground with the code preloaded. Change the numbers, break things, rerun. That is how it sticks.

R is a calculator that remembers

At its core, R evaluates expressions and stores results in named objects with the <- arrow. A collection of values is a vector, made with c() (for combine), and most R functions happily take a whole vector at once.

# Scores from a small pilot study
scores <- c(72, 85, 91, 68, 77, 88, 95, 60)

mean(scores)      # average
sd(scores)        # standard deviation
length(scores)    # how many values

Output

[1] 79.5
[1] 12.24745
[1] 8

Everything after a # is a comment. The [1] in the output is just R labelling the position of the first printed value.

Data lives in data frames

A data frame is R's spreadsheet: rows are observations (participants, trials, samples), columns are variables. R ships with practice datasets, and this course leans on them so every example is reproducible anywhere. mtcars records 11 measurements for 32 cars; head() shows the first rows and str() summarises the structure.

head(mtcars)      # first 6 rows
str(mtcars)       # structure: 32 obs. of 11 variables

You reach into a data frame with the $ sign: mtcars$mpg is the fuel-efficiency column as a plain vector, ready for any function.

mean(mtcars$mpg)
range(mtcars$mpg)

Output

[1] 20.09062
[1] 10.4 33.9

Your own data would load the same way

With R installed on your computer, one line reads a CSV exported from Excel, Qualtrics, or a lab instrument into a data frame. (It needs a file on disk, so it will not run in the browser playground, everything else in this course will.)

R
mydata <- read.csv("study_data.csv")
head(mydata)

What you can do already

  • Store values in named objects with <-
  • Build vectors with c() and summarise them with mean(), sd(), length()
  • Inspect any data frame with head() and str()
  • Pull out a column with $

Next: turning raw columns into the descriptive statistics every results section starts with.

Ready to turn your spreadsheet into results?

Create an account and run your first analysis in minutes. No install, no statistics course required.