1、grep
1 ## a为一个data frame,取含有RNA-Seq的行
2 index <- grep("RNA-Seq", a$Assay_Type)
3 b <- a[index,]
2、读取文件,选择不读哪一行
1 ##不读取带有!的行
2 data <- read.table("file",comment.char="!", sep="\t")
3、去掉数据框的某一列,添加行名, 列名
##删除第一列
b<-b[,-1]
##添加行名,列名
rowname(b) <- b[,1]
colname(b) <- b[1,]
4、sort
1 ##a为一data frame
2 ##最小值
3 sort(a$num)[1] ; 等价min(a$num)
4 ##四分位
5 sort(a$num, decreasing=T)[1] ; 等价max(a$num)
6 ##five number, 最小值,下四分位,中位数,上四分位,最大值
7 fivenum(a$num)
8 ##<5000的行
9 b <- a[a$num < 5000,]
5、strsplit 切割
1 ##以2进行分割
2 b <- "adfdfd2jdfkd"
3 strsplit(b, "2")
4 adfdf, jdfkd
6、t检验
1 t.test(1,2) 查看p value,若<0.05,则显著
7、paste, rep
1 ##a, 连续粘贴4次
2 paste("a",1:5)
3 结果:
4 a 1, a 2, a 3, a 4
5 ##a, 连续粘贴4次,以“-”连接
6 paste("a",1:5, sep='-')
7 结果:
8 a-1, a-2, a-3, a-4
9
10 ##复制a 4次
11 rep("a", 4)
12 结果:
13 a,a,a,a
8、apply 循环
1 ##b 为一个data frame, 每一行求平均数
2 apply(b, 1, function(x){
3 mean(x)
4 })
5 其中 1 代表行,2 代表列
6 上面可以简写为:
7 apply(b, 1, mean)
9、输出图片
1 ## 比如输出PDF图片
2 pdf("test.pdf", width=12, height=10)
3 ggplot(data, aes(x,y)) +geom_line()
4 dev.off
10、quantile()
## 在R语言中取百分位比用quantile()函数
data <- c(1,2,3,4,5,6,7,8,9,10)
quantile(data,0.5)
50%
5.5
quantile(data,c(0.25,0.75))
25% 75%
3.25 7.75
p + geom_hline(yintercept =quantile(data, 0.95) ,color="blue",linetype=2,size-0.5)
11、which()
1 ## which 可以筛选数据库中特有的行,比如有数据框data,筛选出delt大于0的行
2 zheng <- data\[which(data$delt>0),]
12、melt(),dcast()
1 ## melt() 和 dcast()为reshape2中的命令
2
3 ## melt(): 可以将长的数据框变为短的
4 > head(new_data)
5 pos xx_mean xy_mean yy_mean
6 19 36001 1.4052965 0.8884643 1.0787320
7 20 38001 0.8538059 1.0241777 1.1718369
8 21 40001 1.7150511 1.5263786 1.4663535
9 23 44001 0.0000000 1.8001673 0.9257189
10 24 46001 0.2837331 1.5285861 1.5567648
11 25 48001 0.6670272 2.0511266 0.0000000
12
13 > head(melt(new_data,id.vars = "pos"))
14 pos variable value
15 1 36001 xx_mean 1.4052965
16 2 38001 xx_mean 0.8538059
17 3 40001 xx_mean 1.7150511
18 4 44001 xx_mean 0.0000000
19 5 46001 xx_mean 0.2837331
20 6 48001 xx_mean 0.6670272
21
22 以“pos”为不变的ID列,其它变量合并在variable列中,其对应的值为value列
23
24 ## dcast(): 可以将短的数据框变为长的
25 > head(test)
26 pos variable value
27 1 36001 xx_mean 1.4052965
28 2 38001 xx_mean 0.8538059
29 3 40001 xx_mean 1.7150511
30 4 44001 xx_mean 0.0000000
31 5 46001 xx_mean 0.2837331
32 6 48001 xx_mean 0.6670272
33
34 > head(dcast(test,pos~variable))
35 pos xx_mean xy_mean yy_mean
36 1 36001 1.4052965 0.8884643 1.0787320
37 2 38001 0.8538059 1.0241777 1.1718369
38 3 40001 1.7150511 1.5263786 1.4663535
39 4 44001 0.0000000 1.8001673 0.9257189
40 5 46001 0.2837331 1.5285861 1.5567648
41 6 48001 0.6670272 2.0511266 0.0000000
42
43 ~左边的表示不变的列,右边则展开
---END----
手机扫一扫
移动阅读更方便
你可能感兴趣的文章