# Various matrix operations using the operators in [Table 3-7]# Store two 2×2 matrices in x and y, respectivelyx =array(1:4, dim =c(2, 2))y =array(5:8, dim =c(2, 2))x
# If the center value is 1, apply the function row by rowapply(x, 1, mean)
[1] 5.5 6.5 7.5
# If the center value is 2, apply the function to each columnapply(x, 2, mean)
[1] 2 5 8 11
x =array(1:12, c(3, 4))dim(x)
[1] 3 4
x =array(1:12, c(3, 4))# Randomly mix and extract array elementssample(x)
[1] 6 7 8 12 9 3 1 5 4 11 10 2
# Select and extract 10 elements from the arraysample(x, 10)
[1] 2 1 5 9 7 12 3 8 4 11
library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
# ?sample# The extraction probability for each element can be variedsample(x, 10, prob =c(1:12)/24)
[1] 12 5 11 8 10 4 6 2 3 9
# You can create a sample using just numberssample(10)
[1] 2 3 6 4 8 7 1 5 9 10
Class
Create a new project
*.Rproj
*.R
getwd()
Variable and Object
An object in R is a data structure used for storing data: Everything in R is an object, including functions, numbers, character strings, vectors, and lists. Each object has attributes such as its type (e.g., integer, numeric, character), its length, and often its dimensions. Objects can be complex structures, like data frames that hold tabular data, or simpler structures like a single numeric value or vector.
A variable in R is a name that you assign to an object so that you can refer to it later in your code. When you assign data to a variable, you are effectively labeling that data with a name that you can use to call up the object later on.
Here’s a simple example in R:
my_vector <-c(1, 2, 3)
my_vector is a variable. It’s a symbolic name that we’re using to refer to some data we’re interested in.
c(1, 2, 3) creates a vector object containing the numbers 1, 2, and 3.
This vector is the object, and it’s the actual data structure that R is storing in memory.
# remove all objects storedrm()# Create a vector 1 to 101:10
[1] 1 2 3 4 5 6 7 8 9 10
# Sampling 10 values from the vector 1:10sample(1:10, 10)
[1] 8 4 5 9 10 7 6 1 2 3
X <-sample(1:10, 10)# Extract 2nd to 5th elements of XX[2:5]
[1] 5 10 3 8
Vectorized codes
c(1, 2, 4) +c(2, 3, 5)
[1] 3 5 9
X <-c(1,2,4,5)X *2
[1] 2 4 8 10
Recycling rule
1:4+c(1, 2)
[1] 2 4 4 6
X<-c(1,2,4,5)X *2
[1] 2 4 8 10
1:4+1:3
Warning in 1:4 + 1:3: longer object length is not a multiple of shorter object
length
[1] 2 4 6 5
Pop-up Qz
Choose two if its type cannot be ‘factor’ variable in R
Arrays are a fundamental data structure in R that extend vectors by allowing you to store multi-dimensional data. While a vector has one dimension, arrays in R can have two or more dimensions, making them incredibly versatile for complex data organization.
What is an Array in R?
An array in R is a collection of elements of the same type arranged in a grid of a specified dimensionality. It is a multi-dimensional data structure that can hold values in more than two dimensions. Arrays are particularly useful in scenarios where operations on multi-dimensional data are required, such as matrix computations, tabulations, and various applications in data analysis and statistics.
Creating an Array
To create an array in R, you can use the array function. This function takes a vector of data and a vector of dimensions as arguments. For example:
# Create a 2x3 arraymy_array <-array(1:6, dim =c(2, 3))print(my_array)
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
This code snippet creates a 2x3 array (2 rows and 3 columns) with the numbers 1 to 6.
Accessing Array Elements
Elements within an array can be accessed using indices for each dimension in square brackets []. For example:
# Access the element in the 1st row and 2nd columnelement <- my_array[1, 2]print(element)
[1] 3
Modifying Arrays
Just like vectors, you can modify the elements of an array by accessing them using their indices and assigning new values. For example:
# Modify the element in the 1st row and 2nd column to be 20my_array[1, 2] <-20print(my_array)
[,1] [,2] [,3]
[1,] 1 20 5
[2,] 2 4 6
Operations on Arrays
R allows you to perform operations on arrays. These operations can be element-wise or can involve the entire array. For example, you can add two arrays of the same dimensions, and R will perform element-wise addition.
Example: Creating and Manipulating a 3D Array
# Create a 3x2x2 arraymy_3d_array <-array(1:12, dim =c(3, 2, 2))print(my_3d_array)
# Access an element (2nd row, 1st column, 2nd matrix)element_3d <- my_3d_array[2, 1, 2]print(element_3d)
[1] 8
Quiz: Test Your Understanding of Arrays in R
Question 1: What is the output when accessing the third element in the second row and first column of a 3x3x3 array filled with elements from 1 to 27?
A) 3
B) 12
C) 21
D) 9
Question 2: Which of the following statements creates a 2x2x3 array containing the numbers 1 through 12 in R?
A) array(1:12, dim = c(2, 2, 3))
B) matrix(1:12, nrow = 2, ncol = 2)
C) c(1:12)
D) array(1:12, dim = c(3, 2, 2))
Question 3: How do you modify the element at position [1, 1, 1] in a 3-dimensional array named ‘arr’ to have a value of 100?
A) arr[1] <- 100
B) arr[1, 1, 1] <- 100
C) arr[c(1, 1, 1)] <- 100
D) Both B and C are correct.
Answers:
Answer 1: B) 12 Explanation: Arrays in R are filled column-wise, so the third element in the second row and first column of the second matrix would be 12.
Answer 2: A) array(1:12, dim = c(2, 2, 3)) Explanation: The array function with dimension argument c(2, 2, 3) will create a 2x2x3 array, filling the elements from 1 to 12 across the dimensions.
Answer 3: B) arr[1, 1, 1] <- 100 Explanation: To modify a specific element in an array, you need to specify all its indices. The correct way is arr[1, 1, 1] <- 100.
COV19 matrix and visualization
Data import
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats 1.0.0 ✔ readr 2.1.4
✔ ggplot2 3.4.4 ✔ stringr 1.5.0
✔ lubridate 1.9.3 ✔ tibble 3.2.1
✔ purrr 1.0.2 ✔ tidyr 1.3.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
library(rvest)
Attaching package: 'rvest'
The following object is masked from 'package:readr':
guess_encoding
library(stringdist)
Attaching package: 'stringdist'
The following object is masked from 'package:tidyr':
extract
library(reshape2)
Attaching package: 'reshape2'
The following object is masked from 'package:tidyr':
smiths
Rows: 289 Columns: 1147
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): Province/State, Country/Region
dbl (1145): Lat, Long, 1/22/20, 1/23/20, 1/24/20, 1/25/20, 1/26/20, 1/27/20,...
ℹ 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.
Rows: 289 Columns: 1147
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): Province/State, Country/Region
dbl (1145): Lat, Long, 1/22/20, 1/23/20, 1/24/20, 1/25/20, 1/26/20, 1/27/20,...
ℹ 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.