-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
512 lines (512 loc) · 14.2 KB
/
.Rhistory
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
to = ti %>% filter(gid %in% gids) %>%
spread(SampleID, val)
cat(sprintf("%d genes passed filtering\n", nrow(to)))
write_tsv(to, fo)
library("argparse")
import argparse
install argparse
install.packages("argparse")
library(argparse)
library("argparse")
#!/usr/bin/env Rscript
suppressPackageStartupMessages(library("argparse"))
p <- ArgumentParser(description = 'Filter expression matrix for network analysis')
p$add_argument("fi", nargs=1, help="Input file with expression matrix (cpm.rds)")
p$add_argument("fo", nargs=1, help="output file (*.tsv)")
p$add_argument("--subid", default='z',
help="subset ID used to filter expression matrix [default: %(default)s]")
p$add_argument("--num_sam_on", type='integer', default=0,
help="a gene needs to be expressed in at least XX samples [default: %(default)s]")
p$add_argument("--pct_sam_on", type='double', default=0,
help="a gene needs to be expressed in at least XX%% of samples [default: %(default)s]")
p$add_argument("--min_cpm", type='double', default=1,
help="CPM threshold to determine expression on/off [default: '%(default)s']")
p$add_argument("--min_var_p", type='double', default=0,
help="genes with variance below this percentile will be removed [default: '%(default)s']")
p$add_argument("--no_asinh", action='store_true',
help="use raw CPM/FPKM values (no asinh transformation) [default: '%(default)s']")
p$add_argument("--use_fpkm", action='store_true',
help="use FPKM instead of CPM [default: '%(default)s']")
args <- p$parse_args()
fi = args$fi
fo = args$fo
subid = args$subid
if( file.access(fi) == -1 )
stop(sprintf("file ( %s ) cannot be accessed", fi))
source("~/projects/grn/src/functions.R")
x = readRDS(fi)
tm = x$tm_m
if(subid != 'z') {
if(subid %in% x$th_m$Tissue) {
sids = x$th_m %>% filter(Tissue == subid) %>% pull(SampleID)
} else if(subid %in% x$th_m$Genotype) {
sids = x$th_m %>% filter(Genotype == subid) %>% pull(SampleID)
} else {
cat(sprintf("subid [%s] not found\n", subid))
return(1)
}
tm = tm %>% filter(SampleID %in% sids)
}
if(args$use_fpkm) {
ti = tm %>% select(gid, SampleID, val=FPKM)
} else {
ti = tm %>% select(gid, SampleID, val=CPM)
}
cat(sprintf("%d genes in %d samples read\n", length(unique(ti$gid)), length(unique(ti$SampleID))))
if(!args$no_asinh)
ti = ti %>% mutate(val = asinh(val))
tis = ti %>% group_by(gid) %>%
summarise(nsam_on = sum(val >= args$min_cpm),
psam_on = nsam_on/n(),
val_sd = sd(val)) %>%
ungroup()
gids = tis %>%
filter(nsam_on >= args$num_sam_on,
psam_on >= args$pct_sam_on,
val_sd >= 0) %>% pull(gid)
tis2 = tis %>% filter(gid %in% gids)
min_sd = quantile(tis2$val_sd, args$min_var_p)
gids = tis2 %>% filter(val_sd >= as.numeric(min_sd)) %>% pull(gid)
to = ti %>% filter(gid %in% gids) %>%
spread(SampleID, val)
cat(sprintf("%d genes passed filtering\n", nrow(to)))
write_tsv(to, fo)
library("ggplot2")
library("amplicon")
library("ggplot2")
library("amplicon")
library("amplico")
a <- d nalda
#####################################
## 基本的数据类型介绍 ###
#####################################
data() #显示R语言内置的数据集 按照字母顺序排列
class(rivers) #显示类型 数字类型的向量可以绘制 点图 饼图 等等
cd
pwd
clear
library(ggplot2)
library("ggplot2")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
pavian::runApp(port=5000)
shiny::runGitHub("fbreitwieser/pavian", subdir = "inst/shinyapp")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
install.packages("pavian")
install.packages('pavian', dependencies=TRUE, repos='http://cran.rstudio.com/')
install.packages("pavian", dependencies=TRUE, repos='http://cran.rstudio.com/')
source("http://bioconductor.org/biocLite.R")
#指定一个离你最近的国内镜像
options(BioC_mirror="http://mirrors.ustc.edu.cn/bioc/")
biocLite("pavi")
source("http://bioconductor.org/biocLite.R")
#指定一个离你最近的国内镜像
options(BioC_mirror="http://mirrors.ustc.edu.cn/bioc/")
biocLite("pavian")
pavian::runApp(port=5000)
pavian::runApp(port=5000)
find.package("pavian")
pavian::runApp(port=5000)
library(pavian)
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
library(pavian)
library(pavian)
shiny::runGitHub("fbreitwieser/pavian", subdir = "inst/shinyapp")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
shiny::runGitHub("fbreitwieser/pavian", subdir = "inst/shinyapp")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
shiny::runGitHub("fbreitwieser/pavian", subdir = "inst/shinyapp")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
shiny::runGitHub("fbreitwieser/pavian", subdir = "inst/shinyapp")
install_github()
devtools::install_github("mne-tools/mne-r", INSTALL_opts=c("--no-multiarch"))
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("Rsamtools")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
pavian::runApp(port=5000)
pavian::runApp(port=5000)
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("Rsamtools")
pavian::runApp(port=5000)
if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("fbreitwieser/pavian")
pavian::runApp(port=5000)
pwd
ls
ll
#矩阵,数据框,时间序列等
help(package="databas")
#矩阵,数据框,时间序列等
help(package="database")
#矩阵,数据框,时间序列等
help(package="dataset")
#矩阵,数据框,时间序列等
help(package="datasets")
data()
rivers
rivers <- (1,2,3)
rivers <- c(1,2,3)
rivers
data()
rivers
data("rivers")
rivers
help("mtcars")
#内置数据集有资料,自行查看
names(euro)
names(rivers)
state <- data.frame(state.name,state.abb,state.area,state.division,state.region) #将这5个数据集构成一个数据框
state
state.x77
iris3
heatmap(iris)
heatmap(volcano)
iris
Titanic
UCBAdmissions
crimtab
eurodist
mtcars
sleep #两种药物的催眠效果
women #15个女性的身高和体重
#时间序列数据,可以反应出变化的趋势
co2
presidents
help("car93")
help("Car93")
help("Cars93")
??cars93
data(package="MASS")
data(package=.packages(all.available = TRUE))
Chile
data(Chile)
data(Chile,package = "car")
cars
data(Chile,package = "carData")
Chile
x <- c(1,2,3,4,5,6)
x
print(x)
y <- c("one","two","three")
y
z <- c(TRUE,FALSE)
z
w <- c(T,F,T,T)
w <- c(T,F,T,T)
w
c(1:100)
c(T,F,T,T)
seq(1,100,by = 3)
seq(1,100,by = 3)
seq(from=1,to=100,by=4)
seq(1,100,length.out = 10)
?rep
rep(2,5)
rep(x,5)
rep(x,4)
rep(x,3)
rep(x,5)
x <- c(1,2,3,4,5)
rep(x,5)
rep(x,each=5)
rep(x,each=5,times=2)
a <- c(1,2,"one")
a
mode(a)
mode(z)
a;b;c;d
a=2
b=3
c='hello'
d=TRUE
a;b;c;d
iris
iris3
state.x77
heatmap(state.x77)
#数据框本质是一种列表
#数据框特征:数据框每一列必须为同一类型,每一行可以不同
iris
mtcars
??mtcars
?mtcars
?mtcars
rock
#数据框的创建
?dateframe
#数据框的创建
?date.frame
#数据框的创建
?data.frame()
state.name;state.abb;state.region;state.x77;
state.name;state.abb;state.region;state.x77
#将这些矩阵转换为数据框
state <- data.frame(state.name,state.abb,state.region.state.x77)
#将这些矩阵转换为数据框
state <- data.frame(state.name,state.abb,state.region,state.x77)
state
state <- data.frame(state.name,state.abb,state.region,state.x77)
state
#数据框的访问
state[1]
state[2,4]
state[c(2,4)]
state[-c(2,4)]
state[c(-2,4)]
state[,"state.abb"]
state["Wyoming",]
state.Murder
state$Murder
state$Wyoming
state$state.name
state$Illiteracy
#案例
wemen
#案例
women
plot(women)
plot(women$height,women$weight)
plot(women$height,women$weight)
plot(women)
#进行线性回归
lm(weights~height,data = women)
#进行线性回归
lm(weight~height,data = women)
#attach函数加载数据框
?attach
mtcars
mtcars[1]
attach(mtcars)
mpg
hp
colnames(mtcars)
carb
cyl
rownames(mtcars)
detach(mtcars)
cyl
mtcars["cyl"]
mtcars[,"cyl"]
mtcars$mpg
#with函数
with(mtcars)
#with函数
with(mtcars,{hp})
with(mtcars,{sum(mpg)})
sum(mtcars[,"cyl"])
mtcars$mpg
mtcars[cyl]
mtcars["cyl"]
mtcars[["cyl"]]
mtcars[["cyl"]]
#在R中名义型变量和有序性变量称为因子(统计学知识)
#因子的应用:计算频数,独立性检验,相关性检验,方差检验,方差分析,主成分分析,因子分析
mtcars
#在R中名义型变量和有序性变量称为因子(统计学知识)
#因子的应用:计算频数,独立性检验,相关性检验,方差检验,方差分析,主成分分析,因子分析
mtcars
#在R中名义型变量和有序性变量称为因子(统计学知识)
#因子的应用:计算频数,独立性检验,相关性检验,方差检验,方差分析,主成分分析,因子分析
mtcars[,cyl]
#在R中名义型变量和有序性变量称为因子(统计学知识)
#因子的应用:计算频数,独立性检验,相关性检验,方差检验,方差分析,主成分分析,因子分析
mtcars[,"cyl"]
mtcars$cyl
?mtcars
table(mtcars$cyl)
f <- factor(c("red","yellow","blue","red","blue"))
f
weekdays()
weekdays.Date()
weekdays.Date(x)
week <- factor(c("Mon","Fri","Thu","Wen","Fri","Sun"))
week
week <- factor(c("Mon","Fri","Thu","Wen","Fri","Sun"))
week
#人为指定level水平
week <- factor(c("Mon","Fri","Thu","Wen","Fri","Sun"),order =TRUE,
levels = c("Mon","Tue","Wed","Thu","Fri","Sat","Sun"))
week
fcyl <- factor(c(mtcars$cyl))
fcyl
table(mtcars$cyl)
plot(mtcars$cyl)
plot(fcyl)
#cut函数简介
num <- c(1:100)
num
cut(num,c(seq(1:100,10)))
cut(num,c(seq(0:100,10)))
cut(num,c(seq(1:100,9)))
cut(num,c(seq(1:100,1)))
cut(num,c(seq(1,100,10)))
cut(num,c(seq(1,100,11)))
cut(num,c(seq(1,100,9)))
plot(num)
plot(cut(num,c(seq(1,100,9))))
#其他类型的因子数据
state.division
plot(state.division)
plot(state.division,margin=0.1)
plot(state.division,uniform=0.1)
plot(state.division,uniform)
plot(state.division)
state.region
plot(state.region)
plot(state.division)
class(state.region)
#在R中NA代表缺失值(not available)不可用,注意,没有不代表就是0
x <- c(1:9)
x
x[10]=10
x[10] <- 10
x
x[12] <- 12
x
x <- c(1:9)
x
x[12] <- 12
x
x <- c(1:9)
x
x[11] <- 11
x
1+NA
NA==0
a <- c(NA,c(1:49))
a
a <- c(NA,c(1:49))
a
sum(a)
mean(a)
sum(a,na.rm = TRUE)
mean(a,na.rm = TRUE)
mean(1:49)
is.na(a)
library(VIM)
sleep
is.na(sleep)
is.na(sleep)
sleep
colSums(sleep)
rowsum(sleep)
rowSums(sleep)
rowSums(sleep)
#将NA值移除的第二种方法
a <- c(1:9)
a
a[11] <- 11
a
na.omit(a)
a
a <- na.omit(a)
a
mean(a)
sum(a)
na.omit(sleep)
# na.omit()对于数据框的应用
na.omit(sleep) #将含有确实值的行删除
rownames(sleep)
legend(rownames(sleep))
length(rownames(sleep))
length(rownames(na.omit(sleep)))
#其他缺失数据
#缺失数据NaN,代表是不存在的值
#lnf代表无穷(分为正无穷和负无穷)
1/0
-1/0
0/0
0/10
is.nan(0/0)
0/0
is.infinite(1/0)
df -h
?iris
head(iris)
iris
iris
head(iris)
iris
?saveRDS
save(iris,file = "iris.RDS")
getwd()
dir()
setwd("C:/Users/14988/Desktop/R语言学习")
save(iris,file = "iris.RDS")
dir()
readRDS("iris.RDS")
x <- readRDS("iris.RDS")
?RDS
??RDS
readRDS()
?readRDS()
x <- readRDS(file = "iris.RDS")
x <- readRDS(file = "iris.RDS",refhook = NULL)