-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathR tutorial 1.R
373 lines (257 loc) · 8.94 KB
/
R tutorial 1.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
### ------------------------------------- ###
### Intro to R ###
### ------------------------------------- ###
# rm(list = ls()) ## this is the code to empty the global environment in R
### ------------------------------------- ###
# 1. R installation #
### ------------------------------------- ###
# https://www.r-project.org/
### ------------------------------------- ###
# 2. R studio #
### ------------------------------------- ###
# https://www.rstudio.com/
### ------------------------------------- ###
# 3. Common data types #
### ------------------------------------- ###
## Numeric:
x1<-c(1:10)
print(x1)
x1
x1[1]
## Character:
x2<-c("A","B","C","*","/30-+gle","1","6")
print(x2)
x2
x2[5]
## Logical:
class(FALSE)
class(NA)
# NA is a data type/indicator represents the absence of a value, and is represented by the keyword NA (without quotes)
## Vector: x1 and x2 are both a vector
## List: an object contains heterogeneous elements. The elements can be matrices, data frames, functions.
## For example, we can save x1 and x2 with different lengths to a list:
length(x1)
length(x2)
x1x2_list=list(var1=x1,var2=x2) # var1 and var2 are the variable/column names given to x1 and x2, respectively.
### ------------------------------------- ###
# 4. Operators for basic calculation: #
### ------------------------------------- ###
## Addition
20+50
## Subtraction
8-5
## Multiplication
2*10
## Division
10/5
## Exponentiation
10^3
## Modulo: returns the reminder of a division
25%%7
## Assign data to variables:
x1=c(1,1,1)
x2=c(2,1,1)
x3=c(1,0.5,1)
x4="five"
y1=1
y2=0.5
y3=3
x1+x2
x1*y1
x1+x4
## Multiplication in matrices
### Build a matrix from vectors
m1=rbind(x1,x2,x3)
### Build a matrix from scratch
m2=matrix(data = 2, nrow = 3, ncol = 2 )
### Build a vector from elements
v1=c(y1,y2,y3)
m1/2
m1[,1]/2
m1[1,]/2
m1/v1[1]
m1/v1[2]
m1[,1]/v1[1]
m1[1,]/v1[2]
m1[3,3]/v1[3]
m1 * v1
m1 %*% v1
m1 * m2
m1 %*% m2
### ------------------------------------- ###
# 5. Import data sets #
### ------------------------------------- ###
setwd("/YOUR DATA LOCATION/")
## Import CSV data file
data_csv=read.csv("Data 1/binary.csv", header = TRUE, stringsAsFactors = FALSE )
## Import CSV data from an online link, e.g., GitHub repository
data_csv.GitHub=read.csv("https://raw.githubusercontent.com/cmomo/IntroR/main/Data 1/binary.csv", header = TRUE, stringsAsFactors = FALSE )
## Import text data file
data_txt=read.table("Data 1/binary.txt", header = TRUE, stringsAsFactors = FALSE )
## Import Excel data file (need a package called "openxlsx" to import)
### Install required package:
install.packages("openxlsx")
### Check whether the required package has already been installed. If not, install the package:
if(length("openxlsx")) install.packages("openxlsx")
### 2 ways to call the installed package
require(openxlsx)
library(openxlsx)
## Import Excel data file:
data_xlsx=read.xlsx("Data 1/binary.xlsx", sheet=1 ) # import the 1st sheet by its order number
data_xlsx=read.xlsx("Data 1/binary.xlsx", sheet="binary" ) # import the 1st sheet by its name
### ------------------------------------- ###
# 6. Data inspection #
### ------------------------------------- ###
## Let's check the dimension of the dataset using data_csv:
dim(data_csv)
nrow(data_csv)
ncol(data_csv)
## We can obtain the column and row names, also change the names accordingly.
colnames(data_csv)
colnames(data_csv)[1]="status"
colnames(data_csv)
colnames(data_csv)[c(2:4)]=c("GRE", "GPA", "ranking")
colnames(data_csv)
colnames(data_csv)=c("admit","gre","gpa","rank" )
colnames(data_csv)
rownames(data_csv)
## Let's find out the class of each variable (i.e., column):
class(data_csv)
class(data_csv$admit)
class(data_csv$gre)
class(data_csv$gpa)
class(data_csv$rank)
class(data_csv[,1])
class(data_csv[,"admit"])
## A handy function to check the class for all variables in the dataset:
lapply(data_csv, class)
sapply(data_csv, class)
# lappy - When you want to apply a function to each element of a list in turn and get a list back.
# sapply - When you want to apply a function to each element of a list in turn, but you want a vector back, rather than a list.
## Change the class of variable
class(data_csv$admit)
adm_numeric=data_csv$admit
adm_factor=as.factor(data_csv$admit)
adm_character=as.character(data_csv$admit)
head(adm_numeric)
head(adm_factor)
head(adm_character)
adm_numeric1=as.numeric(adm_factor)
head(adm_numeric1)
adm_character1=as.character(adm_factor)
head(adm_character1)
adm_numeric2=as.numeric(as.character(adm_factor))
head(adm_numeric2)
### ------------------------------------- ###
# 7. Try some calculations using "data_csv" #
### ------------------------------------- ###
## Calculate the difference between GRE score and the mean GRE score:
mu=mean(data_csv$gre)
data_csv$gre1=data_csv$gre-mu
## Calculate the difference between GRE score and the mean GRE score in each Admit group:
mu1=mean(data_csv$gre[data_csv$admit==1])
mu0=mean(data_csv$gre[data_csv$admit==0])
data_csv$gre1_byAdmit=ifelse(data_csv$admit==1, data_csv$gre-mu1, data_csv$gre-mu0)
### ------------------------------------- ###
# 8. Descriptive of data #
### ------------------------------------- ###
## If a vector of IDs are available for all subjects, and they are saved in the order as the data set.
## We want to combine the IDs with the data set.
ID=ids::random_id(n = 400, bytes = 3, use_openssl = TRUE)
data=data_csv
data1=data
data1$ID=ID
data2=cbind(ID,data)
head(data2)
sapply(data2, class)
## admit: whether a subject was admitted or not (0 = Not Admit; 1 = Admit)
data2$admit_group[data2$admit==0]="Not Admit"
head(data2)
table(data2$admit_group)
data2$admit_group[is.na(data2$admit_group)]="Admit"
head(data2)
table(data2$admit_group)
data2_1=data2[data2$admit==1,]
data2_0a=data2[data2$admit==0,]
data2_0b=data2[data2$admit!=1,]
mean(data2$gre)
mean(data2$gpa)
mean(data2_1$gre)
mean(data2_0a$gre)
mean(data2$gre[data2$admit_group=="Admit"] )
mean(data2$gre[data2$admit_group!="Admit"] )
aggregate(data2$gre, by=list(data2$admit_group), FUN=mean )
sd(data2$gpa)
aggregate(data2$gre, by=list(data2$admit_group), FUN=sd )
meanSD=function(x){
a=mean(x)
b=sd(x)
return(c(Mean=a, SD=b ))
}
aggregate(data2$gre, by=list(data2$admit_group),
FUN=meanSD)
sapply(data2[,-c(1,6)], meanSD)
sapply(data2[, !colnames(data2) %in% c("ID","admit_group")], meanSD)
## Group GPA to A+ >= 3.5, 3<= A- <3.5, 2.5<= B+ <3, 2 <= B- < 2.5
data2$gpa_group=NA
data2$gpa_group[data2$gpa>=3.5]="A+"
data2$gpa_group[data2$gpa<3.5 & data2$gpa>=3]="A-"
data2$gpa_group[data2$gpa<3 & data2$gpa>=2.5]="B+"
data2$gpa_group[data2$gpa<2.5]="B-"
## Check the number of subjects (frequency) in each group:
table(data2$gpa_group)
sum(table(data2$gpa_group))
### ------------------------------------- ###
# 9. Data saving, management, and cleaning #
### ------------------------------------- ###
aggregate(data2[, c("gre")], by=list(data2$admit_group), meanSD)
aggregate(data2[, c("gpa")], by=list(data2$admit_group), meanSD)
gre=aggregate(data2[, c("gre")], by=list(data2$admit_group), meanSD)
gpa=aggregate(data2[, c("gpa")], by=list(data2$admit_group), meanSD)
gre
gpa
## save all results as a list:
res_list=list(GREbyAdmit=gre, GPAbyAdmit=gpa )
write.csv(res_list, file = "res_list.csv")
write.table(res_list, file = "res_list.txt")
saveRDS(res_list, file = "res_list.rds")
sink("res_list_sink.txt")
res_list
sink()
gre$Score="gre"
gre
gpa$Score="gpa"
gpa
gre
gre$x
gre_x=gre$x
gpa_x=gpa$x
gre_x
gpa_x
gre1=cbind.data.frame(Group=gre$Group.1, gre_x )
gpa1=cbind.data.frame(Group=gpa$Group.1, gpa_x )
gre1
gpa1
## Add a column: indicator of test
gre1$Test="GRE"
gpa1$Test="GPA"
gre1
gpa1
res_gre_gpa_c=cbind.data.frame(gre1,gpa1 )
res_gre_gpa_c
res_gre_gpa_r=rbind.data.frame(gre1,gpa1 )
res_gre_gpa_r
res_gre_gpa_r1=res_gre_gpa_r
res_gre_gpa_r1$Mean=round(res_gre_gpa_r1$Mean, digits = 2)
res_gre_gpa_r1$SD=round(res_gre_gpa_r1$SD, digits = 2)
res_gre_gpa_r1
## save cleaned results as a table:
write.csv(res_gre_gpa_r1, file = "res_gre_gpa_r.csv" )
write.csv(res_gre_gpa_r1, file = "res_gre_gpa_r(1).csv", row.names = F )
### ------------------------------------------------------- ###
# 10. Test between two admission groups: admit vs. not admit #
### ------------------------------------------------------- ###
t.test(data2_1$gre, data2_0a$gre, alternative = "two.sided" )
t.test(data2_1$gpa, data2_0a$gpa, alternative = "two.sided" )
wilcox.test(data2_1$gre, data2_0a$gre )
wilcox.test(data2_1$gpa, data2_0a$gpa )