在函数中使用多个变量或在R

时间:2019-09-18 22:42:17

标签: r

我在以前的帖子中找不到我需要的答案,所以我开始一个新的问题。我试图将多个变量输入到for循环或函数中,以获取通过变量的每一步的单个输出。我不需要变量的所有排列,我只需要循环或函数就可以并行地(以相同的顺序)对向量的每个元素进行一次操作。这是我所做的...

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))
  plot<-ggplot(temp,aes(Cluster,Description,size=Count,color=p.adjust))+geom_point()+ggtitle(a)
  print(plot)
}

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

这给了我3次正确的输出,我只想要一次

或者,我一直在尝试这样的事情……

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))
  plot<-ggplot(temp,aes(Cluster,Description,size=Count,color=p.adjust))+geom_point()+ggtitle(title)
  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

要运行我的代码,您将需要非常大的df。但这是一个看起来像的例子。

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, 
    2.40596847523017e-07), p.adjust = c(0.00248177711775767, 
    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")

感谢您提供的任何帮助!

0 个答案:

没有答案