0% found this document useful (0 votes)
32 views4 pages

Week 1 - A.Reviews of Basics

Uploaded by

ACE ALPHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views4 pages

Week 1 - A.Reviews of Basics

Uploaded by

ACE ALPHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

25/01/2024, 12:30 Week 1: Review and Introduction to Data Analysis

Review of basic R
In this section, we revisit basic concepts and usage of R. You may skip the section if you are comfortable
using R.

With R, you can


Compute simple arithmetic operations:

2 / 5 + (5 - 4) * 1

## [1] 1.4

Evaluate logical statements:

5 > 2 && 3 != 5 && 2 == 2

## [1] TRUE

Simulate and plot data:

plot(rnorm(25))

In this class, we will use R to study data.

Variables and data types


You can store values to variables such as
numbers:

my_number <- 5
my_number

## [1] 5

characters:

my_characters <- "this is a single sentence."


my_characters

## [1] "this is a single sentence."

logicals:

my_logical <- TRUE


my_logical

## [1] TRUE

sequences of a single data type called vectors:

https://rconnect.utstat.utoronto.ca/content/a57e9d9b-336e-4f49-9347-afee2f53a192/#section-review-of-basic-r 1/4
25/01/2024, 12:30 Week 1: Review and Introduction to Data Analysis

my_vector <- c(2, 3, 7)


my_vector

## [1] 2 3 7

To extract elements from a vector, you can use


the positional indices inside [ starting with 1:

my_vector[1]

## [1] 2

my_vector[c(1, 3)]

## [1] 2 7

a logical vector of the same length to extract elements at TRUE positions:

my_vector[c(FALSE, FALSE, TRUE)]

## [1] 7

my_vector[c(T, F, T)]

## [1] 2 7

You can create empty vectors of length n with:

n <- 5
numeric(n) # numbers

## [1] 0 0 0 0 0

character(n) # characters

## [1] "" "" "" "" ""

logical(n) # logicals

## [1] FALSE FALSE FALSE FALSE FALSE

Exercise 1
Create a vector that consists of day of your birthday and your first name. Print the vector and comment on its data
type.

R CODE  START OVER  HINT  RUN CODE

1 birth_name <- c(26, "Aditya")


2 birth_name
3

[1] "26" "Aditya"

Functions
In R, a function is in the following form:

<function name>(<arg1>, <arg2>, ...)

You can store the result of a function with result <- function_x(...)

R CODE  START OVER  RUN CODE

1 sum_of_237 <- sum(c(2, 3, 7))


2 sum_of_237
3

https://rconnect.utstat.utoronto.ca/content/a57e9d9b-336e-4f49-9347-afee2f53a192/#section-review-of-basic-r 2/4
25/01/2024, 12:30 Week 1: Review and Introduction to Data Analysis

[1] 12

You can look at the help page of each function with ?<function name> . Whenever you need help using a function,
try the help page first. A quick search on the Internet will often provide many examples as well.

R CODE  START OVER  RUN CODE

1 ?sample
2
3

There are also a plenty of resources and examples online for most R functions.

You can also create custom functions.

R CODE  START OVER  RUN CODE

1 # your function definition may look like ...


2 your_function <- function(arg1, arg2) {
3 # some task using the inputs
4 # the variable names must match the names
5 # defined in the function definition
6 output <- arg1 + arg2
7 # return() explicitly tells R the object your function
8 # the point at which point the function stops and
9 # returns an output
10 return(output)
11 }
12 # you can then call the function as ...
13 your_function(5, 5)

[1] 10

Exercise 2
Create a function named diff_in_absolute_diff() that takes 4 numbers – say, w , x , y , and z — and computes

| w − x | − |y − z |

R CODE  START OVER  HINTS  RUN CODE

1 diff_in_absolute_diff <- function(w, x, y, z) {


2 out <- abs(w - x) - abs(y - z)
3 return(out)
4 }
5 diff_in_absolute_diff(20,10,15,10)
6

[1] 5

Loops and vectorized calls


To loop through each element in a vector, you may use a “for loop” in R:

x <- c(10, 20, 30, 40, 50)


y <- c(5, 10, 15, 20, 25)
z <- numeric(5)
for (i in c(1, 2, 3, 4, 5)) {
z[i] <- x[i] + y[i]
}
z

## [1] 15 30 45 60 75

Many R functions automatically broadcast the operation to each element resulting in a vector of same size.

x + y

## [1] 15 30 45 60 75

Exercise 3

https://rconnect.utstat.utoronto.ca/content/a57e9d9b-336e-4f49-9347-afee2f53a192/#section-review-of-basic-r 3/4
25/01/2024, 12:30 Week 1: Review and Introduction to Data Analysis

Use diff_in_absolute_diff() from Exercise 2 in a loop to compute

| w − x | − |y − z |
for each element of the following vectors.

w <- c(1, 2, 3, 4, 5)
x <- c(10, 20, 30, 40, 50)
y <- c(5, 10, 15, 20, 25)
z <- c(50, 40, 30, 20, 10)

The vectors w , x , y , z , and the function diff_in_absolute_diff() have been predefined for your use in the
following chunk. Use the for-loop to compute and save the resulting values to the vector u .

R CODE  START OVER  HINTS  RUN CODE

1 # use a loop
2 u <- numeric(5)
3 for (i in 1:5) {
4 u[i] <- diff_in_absolute_diff(w[i], x[i], y[i], z[i])
5 }
6 u

[1] -36 -12 12 36 30

In the code chunk below, the vectors are directly used as inputs. Compare the results from the loop
version above.

R CODE  START OVER  RUN CODE

1 # diff_in_absolute_diff() is a vectorized
2 u <- diff_in_absolute_diff(w, x, y, z)
3 u

[1] -36 -12 12 36 30

NEXT TOPIC

https://rconnect.utstat.utoronto.ca/content/a57e9d9b-336e-4f49-9347-afee2f53a192/#section-review-of-basic-r 4/4

You might also like