在R中为for循环分配值

时间:2017-04-28 23:29:05

标签: python r

我尝试使用for循环迭代填充列表。我通常不使用R,但我需要的方法还没有在Python中实现,我也不想自己实现它。代码如下:

path = '/PATH/'
files <- dir(path, pattern = '.txt')

#sampleList <- list()
#pValues <- list()
pValues <- rep(NA, length(files))
sampleList <- rep(NA, length(files))
count = 1
for(file in files){ 
  clonPath = paste('/PATH/', file, sep = '', header = NULL)
  clonal <- read.csv(clonPath, stringsAsFactors = F, sep = ',')
  subs = paste('/PATH/', file, sep = '', header = NULL)
  subPath = paste(subs, '.subclonal.csv', sep = '')
  subclonal <- read.csv(subPath, stringsAsFactors = F, sep = ',')
  colnames(clonal) <- c('X', 'Count', 'Start', 'End', 'Chr')
  colnames(subclonal) <- c('X', 'Count', 'Start', 'End', 'Chr')
  clonalCounts = clonal$Count
  subclonalCounts = subclonal$Count
  if(length(subclonalCounts != length(clonalCounts))){ 

    pValues[count] <- 0
    sampleList[count] <- file
    count = count + 1
    #print(p)

  }
  else{
    p <- chisq.test(subclonalCounts, clonalCounts, simulate.p.value = T) 
    pValues[count]<<-chisq.test(subclonalCounts, clonalCounts, simulate.p.value = T)[3]
    print(chisq.test(subclonalCounts, clonalCounts, simulate.p.value = T)[3])
    sampleList[count] <- file 
    count = count + 1
    print(p[3])
  }
}

基本上,当我运行这段代码时,p的值永远不会更新,而print语句(在调试时很差)(打印不好)不会打印任何东西。我想将chi-sq测试的p值分配给pValues,但它似乎并没有起作用。

编辑:我还手动检查了输入文件的&gt; = 1将使第一个if语句失败,并在else语句中进行评估。

1 个答案:

答案 0 :(得分:0)

这一行:

 if(length(subclonalCounts != length(clonalCounts)))

缺少围绕长度的括号(subclonalCounts)

相关问题