# 在 function 中使用多个变量或在 R 中使用 for 循环

#### using multiple variables in a function OR for loop in R

``````a=c("BP GOterms","CC GOterms","MF GOterms")
i = c(0,2,4)
j = c(2,4,6)
n = rep(n,3)

func<-function(a,i,j,n) {
temp<-arrange(df[df\$ID %in% hit[(i*n+1):(n*j)],], -Cluster,Count)
temp\$Description<-factor(temp\$Description,levels=unique(temp\$Description,fromLast=T))
print(plot)
}

mapply(func,i=i,j=j,n=n,a=a)
``````

``````for (i in c(0,2,4) & j in c(2,4,6) & title in c("BP GOterms","CC GOterms","MF GOterms")) {
temp<-arrange(df[df\$ID %in% hit[(i*n+1):(n*j)],], -Cluster,Count)
temp\$Description<-factor(temp\$Description,levels=unique(temp\$Description,fromLast=T))
print(plot)
}
``````

``````Warning messages:
1: In (i * n + 1):(n * j) :
numerical expression has 81 elements: only the first used
2: In (i * n + 1):(n * j) :
numerical expression has 81 elements: only the first used
3: In Ops.factor(Cluster) : ‘-’ not meaningful for factors
``````

``````structure(list(Cluster = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("M1",
"M2"), class = "factor"), ONTOLOGY = structure(c(1L, 1L, 1L,
1L, 1L, 1L), .Label = c("BP", "CC", "MF"), class = "factor"),
ID = c("GO:1901673", "GO:0034314", "GO:0006892", "GO:0036503",
"GO:0035967", "GO:0006986"), Description = structure(c(1L,
2L, 3L, 6L, 7L, 8L), .Label = c("regulation of mitotic spindle assembly",
"Arp2/3 complex-mediated actin nucleation", "post-Golgi vesicle-mediated transport",
"membrane fusion", "establishment of protein localization to membrane",
"ERAD pathway", "cellular response to topologically incorrect protein",
"response to unfolded protein", "vacuolar transport", "response to topologically incorrect protein",
"cytosolic transport", "endosomal transport", "vacuole organization",
"DNA-dependent DNA replication", "macroautophagy", "response to endoplasmic reticulum stress",
"Golgi vesicle transport", "rRNA processing", "myeloid leukocyte activation",
"rRNA metabolic process", "mitotic nuclear division", "DNA replication",
"regulation of mitotic cell cycle phase transition", "regulation of cell cycle phase transition",
"ribosome biogenesis", "proteasome-mediated ubiquitin-dependent protein catabolic process",
"chromosome segregation", "regulation of chromosome organization",
"autophagy", "process utilizing autophagic mechanism", "proteasomal protein catabolic process",
"ncRNA processing", "mitotic cell cycle phase transition",
"cell cycle phase transition", "regulation of DNA metabolic process",
"negative regulation of cell cycle", "ribonucleoprotein complex biogenesis",
"DNA repair", "ncRNA metabolic process", "regulation of mitotic cell cycle"
), class = "factor"), GeneRatio = c("8/1090", "10/1090",
"18/1090", "18/1090", "20/1090", "21/1090"), BgRatio = c("21/19527",
"32/19527", "95/19527", "95/19527", "97/19527", "104/19527"
), pvalue = c(9.7302693101372e-06, 5.88348341881873e-06,
4.47976958483539e-06, 4.47976958483539e-06, 3.24568370146555e-07,
0.00171451628569576, 0.0014795185682183, 0.0014795185682183,
0.000229701672243719, 0.000198652797104838), qvalue = c(0.00230759974232573,
0.00159418720995361, 0.00137568222478875, 0.00137568222478875,
0.000213580629738545, 0.000184710842940477), Count = c(8L,
10L, 18L, 18L, 20L, 21L)), row.names = c(NA, 6L), class = "data.frame")
``````

1 在R中使用文本变量进行循环

2015-05-28 02:57:00 1 753   r
2 使用R在矩阵中进行循环

2017-11-16 03:31:14 1 55   r
3 使用knn在r中进行变量选择

2013-06-21 21:13:22 1 1133   r/ knn/ roc
4 在变量中进行循环

2014-04-10 18:14:14 3 51   loops
5 如何使用多个循环在矩阵中进行计算？

2017-01-04 19:43:35 1 42   matrix
6 使用 R 循环多个变量

2020-01-22 07:14:38 0 55   r/ loops
7 如何在R中进行操作并返回多个对象的循环

2015-11-24 19:03:05 2 744   r/ loops
8 在R函数中使用for循环

9 如何在 R 中进行 OR 循环

2020-12-16 10:09:24 2 55   r
10 在循环或函数中使用Julia多个图进行绘图