在R中循环2级

时间:2014-04-30 09:51:21

标签: r loops levels

我想使用这个字符串:

probes <- c("BovineHD0500029561","BovineHD1100020616","BTB-00266062","BovineHD0200040828","BovineHD0100013622","BovineHD0100009413","BovineHD0300027356","BovineHD0600029053","BovineHD1500024004","BovineHD0200005954","BovineHD2900000062","BovineHD0200008162","BovineHD0400026684","BovineHD0200037832","BovineHD0300035509","Hapmap40156-BTA-103844","BovineHD0400022157")
chr <- c("chr5", "chr11", "chr12", "chr2", "chr1", "chr1", "chr3", "chr6", "chr15", "chr2", "chr29", "chr2", "chr4", "chr2", "chr3", "chr21", "chr4")

在这个循环中:

{
chr2 <- read.table("LRRadjustedall",chr,".txt", sep=";", header=TRUE)
probe <- c("probes")
pdf("boxplot""probes"".pdf")
boxplot(mat.num[,2], xlab="probes", ylab="LRR", main="probes")
}

我的意思是,重复上述命令17次。第一个包含BovineHD0500029561==probeschr5==chr ...以及最后一个BovineHD0400022157==probeschr4==chr

任何sugestions?干杯!

1 个答案:

答案 0 :(得分:1)

如果对的以下输出是正确的:

for (i in 1:length(probes)) {
  cat(probes[i])
  cat(", ")
  cat(chr[i])
  cat("\n")
}
## BovineHD0500029561, chr5
## BovineHD1100020616, chr11
## BTB-00266062, chr12
## BovineHD0200040828, chr2
## BovineHD0100013622, chr1
## BovineHD0100009413, chr1
## BovineHD0300027356, chr3
## BovineHD0600029053, chr6
## BovineHD1500024004, chr15
## BovineHD0200005954, chr2
## BovineHD2900000062, chr29
## BovineHD0200008162, chr2
## BovineHD0400026684, chr4
## BovineHD0200037832, chr2
## BovineHD0300035509, chr3
## Hapmap40156-BTA-103844, chr21
## BovineHD0400022157, chr4

然后:

for (i in 1:length(probes)) {
  chr2 <- read.table("LRRadjustedall",chr[i],".txt", sep=";", header=TRUE)
  pdf(sprintf("boxplot%s.pdf", probes[i]))
  boxplot(mat.num[,2], xlab=probes[i], ylab="LRR", main="probes")
}

应该适合你,

  • 这是一个真正简单的&#34;如何使用带索引的基本for循环&#34; 问题?
  • 您永远不会在循环中使用chr2