Lesson 2 · Getting started
Objects & functions
Store values, work with lists of numbers, and use functions.
Objects: values with a name
<- stores a value under a name. Read it as "gets".
An object is a labelled box. It appears in the Environment panel.
age <- 21
age
age + 1[1] 21 [1] 22
- Names: letters, numbers,
_and.; start with a letter; no spaces. - Capitals matter:
Ageandageare different. - Shortcut for
<-: Alt+- (Mac: Option+-). You may also see=used the same way.
Vectors: many values
c() combines values into a vector. Here are the exam scores of the first six students in our survey:
exam <- c(73, 75, 52, 43, 88, 67)
exam[1] 73 75 52 43 88 67
Each value has a position. exam[2] picks the highlighted one.
exam[2]
exam[c(1, 3)]
exam[2:4]
exam + 5[1] 75 [1] 73 52 [1] 75 52 43 [1] 78 80 57 48 93 72
2:4 means 2, 3, 4. Maths works on every value at once.
Functions
A function takes something in and gives an answer back.
mean(exam)
median(exam)
sd(exam)
min(exam)
max(exam)
length(exam)[1] 66.33333 [1] 70 [1] 16.36663 [1] 43 [1] 88 [1] 6
Some functions take more than one input (arguments), separated by commas:
round(sd(exam), digits = 1)[1] 16.4
R works from the inside out: first sd(exam), then round it. To see a function's arguments, type ?round in the Console.
Text and TRUE/FALSE
| Type | Example | Note |
|---|---|---|
| number | 21, 3.5 |
for maths |
| text | "Female" |
always in quotes |
| logical | TRUE, FALSE |
no quotes |
gender <- c("Male", "Male", "Female", "Male", "Female", "Male")
gender[1] "Male" "Male" "Female" "Male" "Female" "Male"
Forget the quotes and R looks for an object called Male:
gender <- c(Male, Female)Error: object 'Male' not foundYes/no questions
exam > 70
sum(exam > 70)
exam[exam > 70][1] TRUE TRUE FALSE FALSE TRUE FALSE [1] 3 [1] 73 75 88
| Symbol | Means |
|---|---|
== |
equal to (two =) |
!= |
not equal to |
>, >=, <, <= |
bigger, smaller |
& / | |
and / or |
TRUE counts as 1, so sum() counts how many are true.
Missing values: NA
A skipped answer is NA. Summaries give NA unless you add na.rm = TRUE:
sleep <- c(6, 6, NA, 5.8, 5.5, 6.5)
mean(sleep)
mean(sleep, na.rm = TRUE)[1] NA [1] 5.96
Exercises
These use the first six students in the Student Life Survey.
Your first vector
Their study hours per week were 16, NA, 0, 3, 27, 19 (student 2 didn't answer).
- Save them as
study. - Find the mean study hours, ignoring the missing value.
- What is the largest value?
Show hintHide hint
Use c(). Both mean() and max() need na.rm = TRUE here.
Show answerHide answer
study <- c(16, NA, 0, 3, 27, 19)
mean(study, na.rm = TRUE)
max(study, na.rm = TRUE)[1] 13 [1] 27
Ask questions
Using exam from this lesson:
- Which students scored 65 or more? (TRUE/FALSE)
- How many is that?
- Show only those scores.
Show answerHide answer
exam >= 65
sum(exam >= 65)
exam[exam >= 65][1] TRUE TRUE FALSE FALSE TRUE TRUE [1] 4 [1] 73 75 88 67
Fix the errors
Each line has one mistake. Fix it.
mean(Exam)
round(sd(exam), 2
exam[exam = 88]Show answerHide answer
mean(exam)
round(sd(exam), 2)
exam[exam == 88][1] 66.33333 [1] 16.37 [1] 88
- The object is
exam(small e). - A bracket was missing.
- A question needs
==, not=.
Key points
<-saves a value.c()makes a vector.- Functions:
name(input, argument = value). - Text needs quotes. Questions use
==. - Add
na.rm = TRUEwhen there are missing values.