-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lecture-03 Objects in R Language part-2.R
231 lines (152 loc) · 5.37 KB
/
Lecture-03 Objects in R Language part-2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
################################################################################
# &&&....&&& % Learning R Course in Summer of 2022 #
# &&&&&&..&&&&&& % Teacher: Bo Li, Mingwei Liu #
# &&&&&&&&&&&&&& % Date: Jul. 05th, 2022 #
# &&&&&&&&&&&& % #
# &&&&&&&& % Environment: R version 4.1.1; #
# &&&& % Platform: x86_64-w64-mingw32/x64 (64-bit) #
# & % #
################################################################################
################################################################################
### code chunk number 03: Understanding the data objects and their structure.
################################################################################
### ****************************************************************************
### Step-01. About working directory in R.
# For windows, work directory.
pri.dir <- getwd()
setwd("D:/00-GitHub/LRC/tmp/")
### ****************************************************************************
### Step-02. About objects in R.
# 1) object and its attribute.
# 2) object and its type.
### ****************************************************************************
### Step-03. About vector in R.
# 1) Generating vectors.
# 2) vector
### ****************************************************************************
### Step-04. About matrix in R.
# 1) Generating matrix.
# 2) Accessing the matrix.
### ****************************************************************************
### Step-05. About matrix in R.
# 1) Generating matrix.
# Elements are arranged sequentially by row.
M <- matrix(c(3:14), nrow = 4, byrow = TRUE)
print(M)
# Elements are arranged sequentially by column.
N <- matrix(c(3:14), nrow = 4, byrow = FALSE)
print(N)
# Define the column and row names.
rownames <- c("row1", "row2", "row3", "row4")
colnames <- c("col1", "col2", "col3")
rownames(N) <- rownames
colnames(N) <- colnames
print(N)
P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))
print(P)
# 2) Accessing the matrix.
# Define the column and row names.
rownames = c("row1", "row2", "row3", "row4")
colnames = c("col1", "col2", "col3")
# Create the matrix.
P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))
# Access the element at 3rd column and 1st row.
print(P[1,3])
# Access the element at 2nd column and 4th row.
print(P[4,2])
# Access only the 2nd row.
print(P[2,])
# Access only the 3rd column.
print(P[,3])
### ****************************************************************************
### Step-06. About array in R.
# 1) Generating array.
# Create two vectors of different lengths.
vector1 <- c(5, 9, 3)
vector2 <- c(10, 11, 12, 13, 14, 15)
# Take these vectors as input to the array.
result <- array(c(vector1, vector2), dim = c(3, 3, 2))
print(result)
# 2) Naming Columns and Rows.
# Create two vectors of different lengths.
vector1 <- c(5, 9, 3)
vector2 <- c(10, 11, 12, 13, 14, 15)
column.names <- c("COL1", "COL2", "COL3")
row.names <- c("ROW1", "ROW2", "ROW3")
matrix.names <- c("Matrix1", "Matrix2")
# Take these vectors as input to the array.
result <- array(c(vector1,vector2),dim = c(3,3,2),
dimnames = list(row.names,
column.names,
matrix.names))
print(result)
# ====================================================
x <- matrix(1:100, nrow = 10, byrow = TRUE)
print(x)
rownames(x) <- paste("row", 1:10, sep = "-")
colnames(x) <- paste("col", 1:10, sep = "=")
print(x)
# 3) Accessing array elements.
print(result)
result[1, 1, 1]
result[1, 1, 2]
# Create two vectors of different lengths.
vector1 <- c(5,9,3)
vector2 <- c(10,11,12,13,14,15)
column.names <- c("COL1","COL2","COL3")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
# Take these vectors as input to the array.
result <-
array(
c(vector1, vector2),
dim = c(3, 3, 2),
dimnames = list(row.names,
column.names, matrix.names)
)
# Print the third row of the second matrix of the array.
print(result[3,,2])
# Print the element in the 1st row and 3rd column of the 1st matrix.
print(result[1,3,1])
# Print the 2nd Matrix.
print(result[,,2])
## calculate
x
# 行平均值
mean(x[1, ])
mean(x[2, ])
mean(x[3, ])
help("apply")
apply(x, 1, mean)
apply(x, 2, mean)
mean(x[, 1])
mean(x[, 2])
mean(x[, 3])
mean(x[, 4])
mean(x[, 5])
mean(x[, 6])
apply(x, 1, max)
apply(x, 1, min)
apply(x, 1, var)
# 4) Image and array.
# install EBImage package
install.packages("BiocManager")
library(BiocManager)
BiocManager::install("EBImage")
library(EBImage)
getwd()
img <- readImage("D:/00-GitHub/LRC/data/cqnu-logo.jpg")
print(img)
display(img)
hist(img)
str(img)
[email protected][, , 1][[email protected][, , 1] < 0.5] <- 1
img <- readImage("D:/cqnu-logo.jpg")
[email protected][, , 2][[email protected][, , 2] < 0.5] <- 1
[email protected][1:100, 1:100, 1] <- 0
[email protected][1:100, 1:100, 2] <- 0.5
[email protected][1:100, 1:100, 3] <- 0.8
display(img)
################################################################################
### End of chunk-03.
################################################################################