forked from DrZiruiJ/R_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
for & if & apply
115 lines (61 loc) · 2.32 KB
/
for & if & apply
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
ans<-0 ans <- ans + i # 将每次的值累加
#for循环#
for (i in 1:nrow(rawexprSet)) {
rawexprSet[i,10]<-2^rawexprSet[i,10]
}
#if条件应用#
if (逻辑判断值){
expr1
}else{
expr2
}
#for循环与if条件联合应用#
for (i in 1:nrow(rawexprSet)) {
if ( rawexprSet[i,10]<1){
rawexprSet[i,10]<- -1/rawexprSet[i,10]
}else{
rawexprSet[i,10]
}
}
###箱线图for循环,批量输出表达比较图###
library(ggplot2)
library(ggpubr)
my_comparisons <- list(c("Control","Ischemia"),c("Control","Reperfusion-30"),
c("Control","Reperfusion-120"),c("Ischemia","Reperfusion-30"),
c("Ischemia","Reperfusion-120"))
dir.create("copper boxplot")
#重大创新!!!!
options(digits = 2) #设定显示到小数点后两位
for (i in 1:nrow(gene1)) { #gene1为单列的基因名数据框#
name1=paste0("./copper boxplot/boxplot_",gene1[i,1],".png")
#pdf(file= name1)
p1<-ggboxplot(
dd, x = "group", y = gene1[i,1], #第888888888888处需要改的地方,把CD36改成自己的基因。
color = "group",# palette = c("#00AFBB", "#E7B800"),
add = "jitter")+
stat_compare_means(comparisons = my_comparisons, method = "t.test")
# +theme(axis.text.x = element_text(angle = 30, hjust = 1, vjust = 1) #字体倾斜30度(可加可不加)
ggsave(name1, #较为安全的图片保存格式
p1,
width = 12,
height = 8)
}
#####将结果循环输入excel各个sheet中######
library(rJava)
library(xlsxjars)
library(xlsx)
for ( j in 1:9) {
metadat <- read.xlsx("./meta_R.xlsx", sheetIndex =j)
for (i in 1:nrow(metadat)) {
metadat$Conbine[i]<-paste0(metadat[i,1],metadat[i,4],"/",metadat[i,6])
}
#####下面是以后读入Excel的标准格式####
write.xlsx(metadat, file="file1.xlsx", sheetName=as.character(j), append=TRUE, row.names=FALSE) #####as.character()保证输出的是带引号的字符串很重要######
#####上面是以后读入Excel的标准格式####
}
###### apply函数运用 #######
apply(a,1,function(x){ }) ##a是矩阵,1是行,2是列, x是每一行/列
######用R去除全是0的行#######
X[which(rowSums(X) > 0),]
######merge合并函数#######
d3=merge(x=d1,y=d2,by.x="x2",by.y="y2") #按x的列合并