Chapter 1 Introduction to R
1.1 Intro to R programming (Session 1)
This notebook collects the scripts used for teaching in BBMS1021/BIOF1001 for Introduction to R. You can get this Rmd file on Moodle or here (right-click and “save link as” to download).
R is a programming language, particularly popular for its power in statistical computing, elegant graphics, and also genomic data analysis. It is a free and open-source software, with active support and development from the community. Additionally, R is relatively easy to get started for scientific computing.
Note To learn and practice R programming, you need to install
R
andRStudio
on your computer. You can follow the instructions for installation in the Appendix A chapter.
1.1.1 R as a basic calculator
R can be used like a calculator for arithmetic operations:
Can you write the command to calculate your BMI (i.e. \(mass_{kg} \div {height_m}^2\))?
What is the area of a circle with a radius of 3?
Like any good scientific calculator, R also has bulit-in mathematical functions such as log()
, exp()
, sqrt()
, abs()
, sin()
, cos()
, tan()
, etc.
1.1.2 Variables
In computer programming, variables are used to store information. It is called a variable because you can also change the information that is stored.
# example of assigning variable called age
age <- 18
age
#> [1] 18
name <- "John"
name
#> [1] "John"
Variables can be called just about anything, but there are some rules in R:
They can contain letters (a-z, A-Z), digits (0-9), dots (.), and underscores (_). Special characters like
$
,#
,%
, or spaces are not allowed.A variable name must start with a letter or a dot (.), but if it starts with a dot, the next character cannot be a digit.
Variable names are case-sensitive (e.g.,
age
,Age
, andAGE
are different variables).You cannot use particular words such as
TRUE
,FALSE
,if
,else
, orfunction
as variable names as they are reserved for other purposes in R.
Have a go at create a few variables using different variable names.
As mentioned, variables can reassigned with new values. It can also be assigned the value of another variable. Let’s try this:
# create a variable called height and set to 170
height <- 170
height
#> [1] 170
# now the assign a new value to height
height <- 175
height
#> [1] 175
# now create another variable called new_height and set to 165
new_height <- 165
height <- new_height
height
#> [1] 165
Using R’s calculator capabilities.
Now using variables, can you calculated your BMI?
In R, it is also possible to assign variables using the equals sign =
. However, it is generally recommend to use the left arrow <-
for variable assignment in R.
1.1.3 Data types
All variables in R need to belong to a data type. In R (similar to most other programming languages), there are a few commonly used data types that are predefined in the built-in environment. You can use the class()
and typeof()
functions to check the class and data type of any variable. In total, R has five data types:
- Numeric (e.g. 3.1416, 20)
- Integers (e.g. 5L, -10L)
- Complex (e.g. 3 +2i)
- Logical (e.g. TRUE or FALSE)
- Characters (e.g. “Yuanhua”, ‘Jason’)
1.1.3.1 numeric (or double)
The numeric
data type is for numbers, with or without decimals. It the most commonly used and the default data type in R. The numeric
datatype saves values in double precision (double number of bytes in memory), so the type is also double
.
1.1.3.2 integer
The integer
is another data type used for whole numbers. You can use the capital ‘L’ notation as a suffix to specify a particular value as the integer data type. Also, you can convert a numeric into an integer type using the as.integer()
function.
1.1.3.3 logical
In R, the logical
data type takes either a value of TRUE
or FALSE
. A logical
value is often generated when comparing variables.
1.1.3.4 character
In R, the character
is a data type where you have all the alphabets and special characters. It stores character
values or strings
. Strings in R can contain alphabets, numbers, and symbols. The character type is usually denoted by wrapping the value inside single or double inverted commas.
It is possible to inter-convert between data types. This can be useful as certain operations or functions (we will formally define functions later) only works on certain data types.
# example of converting data types
weight <- 65
typeof(weight)
#> [1] "double"
weight <- as.integer(weight)
typeof(weight)
#> [1] "integer"
score <- "9.5"
typeof(score)
#> [1] "character"
score <- as.numeric(score)
typeof(score)
#> [1] "double"
score
#> [1] 9.5
While R is quite flexible in converting data types, not all conversions are possible. You can experiment with different conversions and we can see what conversions are not permissible.
1.1.4 Functions
As the name suggests, functions are used to perform certain operations on some input. Perhaps without realising, you have already used functions multiple times above. For example, sin()
and typeof()
, etc.
As one more example floor()
is a function here to round a number down to the closest whole number
The functions that we have used above are already predefined by R. But generally, one of the major concepts in computer programming is to write your own functions. Once a function is defined, it can be used over and over again without needing to rewrite the code in the function every time. Consider the following example for make a function to calculate BMI:
# First define a function to calculate BMI
# weight in kg, height in metres
calculate_bmi <- function(weight, height){
bmi <- weight/(height^2)
return(bmi)
}
#Example usage:
jason_weight <- 66.2
jason_height <-1.71
ray_weight <- 68.1
ray_height <- 1.85
calculate_bmi(jason_weight,jason_height)
#> [1] 22.63944
calculate_bmi(ray_weight,ray_height)
#> [1] 19.89774
Now try to write a function to calculate the area of a circle:
The reason why R is such a useful programming language is because many functions to do all kinds of things have already been written for us. We will learn how to access these through R Packages in the next lesson.
1.1.5 R scripts
An R script is a plain text file that contains a series of R commands saved in order. Instead of typing commands one by one directly into the R console, you write them all in this text file. Then, you can run the whole script at once to execute all commands automatically.
R scripts are essential for organsing your work, for automation and for sharing with others.
Actually any of the code blocks above can be saved as a R script, but for clarity (since they are already embedded in this R markdown, I will save the following calculate bmi function separately in a R script file.
# weight in kg, height in metres
calculate_bmi <- function(weight, height){
bmi <- weight/(height^2)
return(bmi)
}
# Example usage
bmi<-calculate_bmi(weight = 66.5,height = 1.72)
print(bmi)
#> [1] 22.47837
Save this as calculate_bmi.R
.
To run the script in the console, we can type:
If you get an error it is most likely the R script was saved in a directory different from the current directory. You can use getwd()
to see whether the current directory the one where you saved the script. If not, you can either use setwd()
to move to the directory where the script is, or alternatively, just resave the R script in the current directory.
Now have a go at writing and saving your own R script that will contain a function to convert your heart rate from beats per minute (bpm) to beats per second (bps), and also print your name and your heart rate in both bpm and bps.
1.1.6 Resource links
This notebook adapts contents from these resources:
- https://www.datamentor.io/r-programming/
- https://www.geeksforgeeks.org/r-data-types/
- https://data-flair.training/blogs/r-data-types/
- http://www.sthda.com/english/wiki/ggplot2-essentials
1.1.6.1 Coding styling
Elegant styling is crucial for maintenance and collaboration. Here is a highly recommended guideline: http://adv-r.had.co.nz/Style.html
1.2 Intro to R programming (Session 2)
You can get this Rmd file on Moodle or here (right-click and “save link as” to download).
In the last session, we started learning to use R through the Rstudio interface.
We also learn about some fundamental programming concepts including Variables, Data types and Functions in R. We also learnt to save our code into Rscripts.
In this lecture, we will delve into data structures in R. We will also learn to read and write files into R.
1.2.1 Data structures
Data structures are one of the most important features in programming. They are used to organise and store data so that we can perform analysis on them quickly and efficiently. Each data structure have pros and cons depending on the type of data it is used to store and the analysis required.
Main data structures used in R:
Vector: A list of items of the same data type, like a series of numbers (numeric) or words (characters).
Matrix: Tables of numbers with rows and columns, where all elements are the same type.
List: Collections that can hold different types of data all together, such as numbers, words, or even other lists.
Data Frame: Like a spreadsheet where each column can have different types of data, useful for tabular data
1.2.2 Vector
Vector is a basic data structure in R. It contains elements in the same data type (no matter double, integer, character or others). You can check the data type by using typeof()
function and the length of the vector by length()
function.
Since a vector has elements of the same type, this function will try and coerce elements to the same type, if they are different. Coercion is from lower to higher types, i.e., from logical to integer to double to a character.
See more introduction here.
# vector of numerics
# c combines numbers in the brackets to a vector
age <- c(16, 20, 15, 17, 18)
age
#> [1] 16 20 15 17 18
typeof (age)
#> [1] "double"
length(age)
#> [1] 5
There are also different ways to automatically generate vectors
# vectors can be created using existing variables
jason <- 1.69
ray <- 1.78
yuanhua <- 1.72
heights <- c(jason, ray, yuanhua)
names(heights) <- c("Jason","Ray","Yuanhua") # you can optionally give names to items in a vector
heights
#> Jason Ray Yuanhua
#> 1.69 1.78 1.72
typeof(heights)
#> [1] "double"
# rep repeats the first number, n times based on the second value
repeats <- rep(3, 5)
repeats
#> [1] 3 3 3 3 3
typeof(repeats)
#> [1] "double"
# generates a vector of integers from 1 to 12 (inclusive)
series <- 1:12
series
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12
typeof(series)
#> [1] "integer"
# generate a vector of random integers by sampling 6 values between 1 and 49 (六合彩)
mark6 <- sample(1:49,6)
mark6
#> [1] 23 32 18 47 35 6
typeof(mark6)
#> [1] "integer"
Vectors can also be combined from other vectors.
# vectors can be combined
new_vector <- c(series,mark6,rep(50,3))
new_vector
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 23 32 18 47 35 6 50 50 50
typeof(new_vector)
#> [1] "double"
Did you notice that new_vector
is now a of type double
?
You can spend a few minutes to create vectors and explore what happens to the data type of the vector when you combine them with others.
# write your code here
myVector <- c(TRUE,2:3,4.0)
myVector
#> [1] 1 2 3 4
typeof(myVector)
#> [1] "double"
1.2.2.1 Coercion
Last week we explored converting the data type of variables using as.numeric
, as.integer
, as.character
etc. These are done explicitly. When combining vectors with different data types they are automatically converted to the most general type in what is known as coercion. It occurs based on the following order:
logical < integer < numeric < character
1.2.2.2 Mathematical operations
Common mathematical operations can be directly performed on numeric or integer vectors:
# example calculations on vectors
steps_D1 <- c(9783, 11233, 7844, 9331)
steps_D2 <- c(12432, 19931, 6833, 8322)
total_steps <- steps_D1 + steps_D2
total_steps
#> [1] 22215 31164 14677 17653
diff_steps <- steps_D1 - steps_D2
diff_steps
#> [1] -2649 -8698 1011 1009
Can you write the code to calculate BMI from the vectors below?
1.2.2.3 Index
Items in a vector can be directly retrieved by their index
# Example of using index to retrieve items in a vector
scores <- c(44,56,63,75,77,78,81,90,93,99)
scores
#> [1] 44 56 63 75 77 78 81 90 93 99
typeof(scores)
#> [1] "double"
scores[5] # retrieve score at index 5 (i.e. 5th position)
#> [1] 77
scores[2:5] # retrieve score from index 2 to 5
#> [1] 56 63 75 77
scores[c(2,5,7)] # retrieve scores from index 2, 5 and 7
#> [1] 56 77 81
scores[-5] # retrieve all scores except index 5
#> [1] 44 56 63 75 78 81 90 93 99
scores[c(TRUE,FALSE)] # retrieve scores in odd positions
#> [1] 44 63 77 81 93
Can you write the code to retrieve the scores from:
- the first position
- even positions
- every 3rd position
1.2.2.4 Modifying a vector
Using the index it is possible to modify specific items in a vector
# example modifying items in a vector
scores <- c(44,56,63,75,77,78,81,90,93,99)
scores
#> [1] 44 56 63 75 77 78 81 90 93 99
scores[5] <- 50 # modify position 5
scores
#> [1] 44 56 63 75 50 78 81 90 93 99
scores[1:4] <- c(30,40,50,60) # modify positions 1 to 4
scores
#> [1] 30 40 50 60 50 78 81 90 93 99
scores[1] <- "Twenty" # modify position 1 and changes vector to character
scores
#> [1] "Twenty" "40" "50" "60" "50" "78" "81" "90"
#> [9] "93" "99"
1.2.3 Matrix
Matrix is a two-dimensional data structure. It is in principle built based on vector but has more functions for matrix computation. It has rows and columns, both of which can also have names. To check the dimensions, you can use the dim()
function.
See more introduction here.
myMatrix <- matrix(1:12, nrow=3)
colnames(myMatrix) <- c("C1","C2","C3","C4") # assign column names
rownames(myMatrix) <- c("R1","R2","R3") # assign row names
myMatrix
#> C1 C2 C3 C4
#> R1 1 4 7 10
#> R2 2 5 8 11
#> R3 3 6 9 12
dim(myMatrix) # prints dimension of the matrix
#> [1] 3 4
1.2.3.1 Indexing a matrix
Accessing items in a matrix is similar to vector but in 2 dimensions
myMatrix <- matrix(1:12, nrow=3)
colnames(myMatrix) <- c("C1","C2","C3","C4")
rownames(myMatrix) <- c("R1","R2","R3")
myMatrix[1, 2] # retrieve item in row 1 column 2
#> [1] 4
myMatrix["R2", "C2"] # retrieve item by row and col name in R2, C2
#> [1] 5
myMatrix[1, 1:2] # retrieve items in row 1 and col 1 and 2.
#> C1 C2
#> 1 4
myMatrix[1:2, c(2, 3)] # retrieve items in row 1-2 and col 2-3
#> C2 C3
#> R1 4 7
#> R2 5 8
1.2.3.2 Modify values
Modifying items in a matrix is similar to vector but in 2 dimensions. Can you write the code to:
replace row 3, column 3 with the value 7?
replace row 1, column 4 using the row and col name with the value -5?
delete row 2
myMatrix <- matrix(1:12, nrow=3)
colnames(myMatrix) <- c("C1","C2","C3","C4")
rownames(myMatrix) <- c("R1","R2","R3")
myMatrix
#> C1 C2 C3 C4
#> R1 1 4 7 10
#> R2 2 5 8 11
#> R3 3 6 9 12
# write code to replace row 3, column 3 with the value 7
myMatrix[3,3] <- 7
myMatrix
#> C1 C2 C3 C4
#> R1 1 4 7 10
#> R2 2 5 8 11
#> R3 3 6 7 12
# write code to replace row 1, column 4 using the row and col name with the value -5
myMatrix["R1","C4"] <- -5
myMatrix
#> C1 C2 C3 C4
#> R1 1 4 7 -5
#> R2 2 5 8 11
#> R3 3 6 7 12
# write code to delete row 2
myMatrix <- myMatrix[-2, ]
myMatrix
#> C1 C2 C3 C4
#> R1 1 4 7 -5
#> R3 3 6 7 12
1.2.4 List
Unlike a vector
, a list
data structure can have components of mixed data types. More broadly, a list
can contain a list of any data structure: value, vector, matrix, etc.
We can use str()
function to view the structure of a list (or any object).
myList <- list(1.70, TRUE, 1:3, "Jason")
str(myList)
#> List of 4
#> $ : num 1.7
#> $ : logi TRUE
#> $ : int [1:3] 1 2 3
#> $ : chr "Jason"
names(myList) <- c("Height","Male","Workdays","Name") # give names to elements in list
str(myList)
#> List of 4
#> $ Height : num 1.7
#> $ Male : logi TRUE
#> $ Workdays: int [1:3] 1 2 3
#> $ Name : chr "Jason"
1.2.4.1 Indexing list
Different from vector and matrix, for a list
, you need to use double-layer square brackets, either by numeric index or name. Alternatively, you can also use $
symbol with the name.
# example of retrieving items from a list
myList[[4]] # using index
#> [1] "Jason"
myList[["Name"]] # using index of Name
#> [1] "Jason"
myList$Name # using the Name key directly
#> [1] "Jason"
Can you retrieve the Height and Name from myList?
# write code to retrieve Height and Name from myList
myList[c(1,4)]
#> $Height
#> [1] 1.7
#>
#> $Name
#> [1] "Jason"
myList[c("Height","Name")] # note that unlike the above, the retrieved values are still in a list as opposed to individual values like what you get with double brackets [[]]
#> $Height
#> [1] 1.7
#>
#> $Name
#> [1] "Jason"
1.2.4.2 Converting to/from vector
It is possible to inter-convert list to and from vectors.
# example of interconverting list to vector and back
myList <- list(1.70, TRUE, 1:3, "Jason")
str(myList)
#> List of 4
#> $ : num 1.7
#> $ : logi TRUE
#> $ : int [1:3] 1 2 3
#> $ : chr "Jason"
myVector <- unlist(myList)
myVector
#> [1] "1.7" "TRUE" "1" "2" "3" "Jason"
myList_cov <- as.list(myVector)
str(myList_cov)
#> List of 6
#> $ : chr "1.7"
#> $ : chr "TRUE"
#> $ : chr "1"
#> $ : chr "2"
#> $ : chr "3"
#> $ : chr "Jason"
1.2.5 Data Frame
Data frame is widely used for rectangular data, where each column has the same data type (vector) but different columns can have different data types (like Excel)
The data frame is in fact a special type of list
: A list of vectors with the same length.
myDF <- data.frame("Height" = c(1.70, 1.80, 1.68, 1.72),
"Weight" = c(67, 60, 55, 58),
"Name" = c("Jason", "Ray", "Dora", "Yuanhua"),
"Gender" = factor(c("male","male","female","male")))
myDF
#> Height Weight Name Gender
#> 1 1.70 67 Jason male
#> 2 1.80 60 Ray male
#> 3 1.68 55 Dora female
#> 4 1.72 58 Yuanhua male
myDF$Name[3] # print name at index 3
#> [1] "Dora"
levels(myDF$Gender) # print levels of gender (which is coded as a factor)
#> [1] "female" "male"
1.2.6 Reading/writing data from/to R
Now that you know how data can be represented in R in different ways you can read in external data to take advantage of its powerful functions.
#install tidyverse (package with lots of functions to handle data in R)
if (!requireNamespace("tidyverse", quietly = TRUE)) { # checks if already installed
install.packages("tidyverse")
}
library(tidyverse) # load the package
#> ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
#> ✔ dplyr 1.1.4 ✔ readr 2.1.5
#> ✔ forcats 1.0.0 ✔ stringr 1.5.1
#> ✔ ggplot2 3.5.2 ✔ tibble 3.3.0
#> ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
#> ✔ purrr 1.1.0
#> ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ dplyr::lag() masks stats::lag()
#> ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
You can download myData.csv
from Moodle or here.
# example of reading in data
getwd()
#> [1] "Z:/jwhwong/programming/BMDS-book/notebooks/chapter1-R"
myData <- read_csv("C:/Users/Jason/Downloads/myData.csv") # use the path where you saved the file on your own computer
#> Rows: 4 Columns: 4
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr (2): Name, Gender
#> dbl (2): Height, Weigth
#>
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
myData
#> # A tibble: 4 × 4
#> Height Weigth Name Gender
#> <dbl> <dbl> <chr> <chr>
#> 1 1.7 67 Jason male
#> 2 1.8 60 Ray male
#> 3 1.68 55 Dora female
#> 4 1.72 58 Yuanhua male
We will modify the first row of data and then write it back to file.
1.3 Intro to R programming (Session 4)
You can get this Rmd file on Moodle or here (right-click and “save link as” to download).
Over the past 2 weeks, you have learnt some of the foundations of computer programming, covering variables, data types, and data structures in R.
In this lecture, we will delve further into developing our knowledge of fundamental concepts in computer programming, covering logic operators, if/else, and loops.
1.3.1 Operators
As the name suggests, operators are symbols that are used to operate on values or variables to produce a result based on the definition of the symbol.
In fact, using operators was one of the first things we did in our first R lectures. When we used R as a calculator, we were applying operators to numbers. For example, when calculating 2 + 2
, the +
sign is an operator that indicates to the programming language that we want to add two numbers together. -
,*
,/
,^
are all mathematical operators
1.3.1.1 Comparison operators
Besides mathematical operators, we often need to make comparisons between numbers or even characters. Common R operators include:
Operator | Description | Example | Returns |
---|---|---|---|
== |
Equal to | 5 == 5 |
TRUE |
!= |
Not equal to | 5 != 7 |
TRUE |
> |
Greater than | 10 > 8 |
TRUE |
< |
Less than | 2 < 4 |
TRUE |
>= |
Greater than or equal to | 5 >= 5 |
TRUE |
<= |
Less than or equal to | 3 <= 5 |
TRUE |
1.3.1.2 Logical operators
Following the a similar idea, Logic operators are used for operating on logic data type (i.e. TRUE or FALSE). The three logical operators are:
AND &
OR |
NOT !
Why do we need logic operators?
In computer programming, we frequently need to evaluate whether some combination of conditions is satisfied before deciding what to do next. Let’s take some real-world examples:
“You will pass the exam if you attend all lectures AND complete all assignments.”
“Jason will do his lecture on Zoom if it is Black Rain OR Typhoon 8.
“I should only drive if I have NOT drunk alcohol.”
Let’s code these in R:
# A function to test if you will pass your exam
passExam <- function(Lectures, Assignments){
return (Lectures & Assignments)
}
passExam(TRUE, TRUE) # attends lectures and does assignments
#> [1] TRUE
passExam(FALSE, TRUE) # only does assignments
#> [1] FALSE
passExam(TRUE, FALSE) # only attends lectures
#> [1] FALSE
passExam(FALSE, FALSE) # does neither
#> [1] FALSE
# A function to test if Jason will have to lecture on Zoom
zoomClass <- function(blackRain, Typhoon8){
return (blackRain | Typhoon8)
}
zoomClass(TRUE, TRUE) # It is Black Rain and Typhoon 8
#> [1] TRUE
zoomClass(FALSE, TRUE) # It is only Typhoon 8
#> [1] TRUE
zoomClass(TRUE, FALSE) # It is only Black Rain
#> [1] TRUE
zoomClass(FALSE, FALSE) # It is neither
#> [1] FALSE
# A function to test if I should drive
drive <- function(alcohol){
return (!alcohol)
}
drive(TRUE) # I have drunk alcohol
#> [1] FALSE
drive(FALSE) # I have not drunk alcohol
#> [1] TRUE
Can you now write a function to evaluate whether someone may have Chikungunya virus infection based on their symptoms of either:
Being over 60 years old with a fever and joint pain.
Having a fever and bitten by a mosquito in the last 5 days.
1.3.2 If/else
While we now know how to use logical operators to evaluate whether a particular condition is TRUE or FALSE , to make use of this in programming, we can link this with an action given the condition.
This is where the if
and else
statement comes in.
# example of if/else based on passing exam
passExam <- function(Lectures, Assignments){
if (Lectures & Assignments){
print("Passed Exam")
} else {
print("Failed Exam")
}
}
passExam(TRUE, TRUE) # attends lectures and does assignments
#> [1] "Passed Exam"
passExam(FALSE, TRUE) # only does assignments
#> [1] "Failed Exam"
passExam(TRUE, FALSE) # only attends lectures
#> [1] "Failed Exam"
passExam(FALSE, FALSE) # does neither
#> [1] "Failed Exam"
It is also possible to chain if/else statements if there are multiple conditions:
# example of multiple if else conditions
ageCategory <- function(age){
if (age <= 12){
print("Child")
} else if (age > 12 & age <= 19){
print("Teenage")
} else if (age > 20 & age <= 60) {
print("Adult")
} else { # > 60
print("Elderly")
}
}
ageCategory(5)
#> [1] "Child"
ageCategory(45)
#> [1] "Adult"
ageCategory(70)
#> [1] "Elderly"
1.3.3 Loop
Another essential computer programming concept is loops. One reason why we need computer programming is for it to complete mundane tasks, such as repeating the same function over and over again on some data.
Here are some simple examples of a for loop in R:
# examples of loop
# Loop printing a series of numbers from 1 to 10
for (i in 1:10){
print(i)
}
#> [1] 1
#> [1] 2
#> [1] 3
#> [1] 4
#> [1] 5
#> [1] 6
#> [1] 7
#> [1] 8
#> [1] 9
#> [1] 10
# Loop printing a vector of names
names <- c("Jason","Ray","Yuanhua")
print(names)
#> [1] "Jason" "Ray" "Yuanhua"
for (i in names){
print(i)
}
#> [1] "Jason"
#> [1] "Ray"
#> [1] "Yuanhua"
# Example of loop printing rows in a data frame
myDF <- data.frame("Height" = c(1.70, 1.80, 1.68, 1.72),
"Weight" = c(67, 60, 55, 58),
"Name" = c("Jason", "Ray", "Dora", "Yuanhua"),
"Gender" = factor(c("male","male","female","male")))
for (i in 1:nrow(myDF)){
print(myDF[i,])
}
#> Height Weight Name Gender
#> 1 1.7 67 Jason male
#> Height Weight Name Gender
#> 2 1.8 60 Ray male
#> Height Weight Name Gender
#> 3 1.68 55 Dora female
#> Height Weight Name Gender
#> 4 1.72 58 Yuanhua male
Now try to write a loop to add each number in vector A to each number in vector B and then only print the result if it is greater than 25.
# here are numbers in vectorA and vectorB
vectorA <- c(3,5,6,7,12,21)
vectorB <- c(-2,4,5,11,15)
# write your code below
There are other types of loops in R, including while
and repeat
loops. We do not have time to go through them today, but you may explore them in your own time.
Later in the course, you will also learn to use apply
which is a more efficient and elegant way to perform repeated functions to certain data structures.