MC³ LabCUHK Psychology

Lesson 2 · Getting started

Objects & functions

Store values, work with lists of numbers, and use functions.

In this lesson

  • Save values with <-
  • Make a vector with c()
  • Use functions like mean() and round()

You need

Scriptlesson-02.RAll the code in this lesson, ready to run.

Objects: values with a name

<- stores a value under a name. Read it as "gets".

age <- 21 → age21

An object is a labelled box. It appears in the Environment panel.

R code
age <- 21
age
age + 1
Output
[1] 21
[1] 22
  • Names: letters, numbers, _ and .; start with a letter; no spaces.
  • Capitals matter: Age and age are 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:

R code
exam <- c(73, 75, 52, 43, 88, 67)
exam
Output
[1] 73 75 52 43 88 67
exam 731752523434885676

Each value has a position. exam[2] picks the highlighted one.

R code
exam[2]
exam[c(1, 3)]
exam[2:4]
exam + 5
Output
[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.

exam73 75 52 43 88 67 → mean( ) → 66.33
R code
mean(exam)
median(exam)
sd(exam)
min(exam)
max(exam)
length(exam)
Output
[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:

R code
round(sd(exam), digits = 1)
Output
[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
R code
gender <- c("Male", "Male", "Female", "Male", "Female", "Male")
gender
Output
[1] "Male"   "Male"   "Female" "Male"   "Female" "Male"  

Forget the quotes and R looks for an object called Male:

R code
gender <- c(Male, Female)
Output
Error: object 'Male' not found

Yes/no questions

R code
exam > 70
sum(exam > 70)
exam[exam > 70]
Output
[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:

R code
sleep <- c(6, 6, NA, 5.8, 5.5, 6.5)
mean(sleep)
mean(sleep, na.rm = TRUE)
Output
[1] NA
[1] 5.96

Exercises

These use the first six students in the Student Life Survey.

Exercise 2.1 Dataset: Student Life Survey (first 6 students)

Your first vector

Their study hours per week were 16, NA, 0, 3, 27, 19 (student 2 didn't answer).

  1. Save them as study.
  2. Find the mean study hours, ignoring the missing value.
  3. What is the largest value?
Show hintHide hint

Use c(). Both mean() and max() need na.rm = TRUE here.

Show answerHide answer
R code
study <- c(16, NA, 0, 3, 27, 19)
mean(study, na.rm = TRUE)
max(study, na.rm = TRUE)
Output
[1] 13
[1] 27
Exercise 2.2 Dataset: Student Life Survey (first 6 students)

Ask questions

Using exam from this lesson:

  1. Which students scored 65 or more? (TRUE/FALSE)
  2. How many is that?
  3. Show only those scores.
Show answerHide answer
R code
exam >= 65
sum(exam >= 65)
exam[exam >= 65]
Output
[1]  TRUE  TRUE FALSE FALSE  TRUE  TRUE
[1] 4
[1] 73 75 88 67
Exercise 2.3 Dataset: Student Life Survey (first 6 students)

Fix the errors

Each line has one mistake. Fix it.

R code
mean(Exam)
round(sd(exam), 2
exam[exam = 88]
Show answerHide answer
R code
mean(exam)
round(sd(exam), 2)
exam[exam == 88]
Output
[1] 66.33333
[1] 16.37
[1] 88
  1. The object is exam (small e).
  2. A bracket was missing.
  3. 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 = TRUE when there are missing values.