【发布时间】:2016-02-05 21:03:31
【问题描述】:
我是 R 新手,需要创建一堆根据他们来自的人口命名的直方图。当我尝试运行没有“名称”部分的循环时,它工作正常。下面的代码循环遍历名称列表并按顺序应用它们,但我最终得到了相同直方图的 3,364 个版本。如果有人有任何建议,我将不胜感激。
popFiles <- list.files(pattern = "*.txt") # generates a list of the files I'm working with
popTables <- lapply(popFiles, read.table, header=TRUE, na.strings="NA")
popNames <- read.table(file.path("Path to file containing names", "popNamesR.txt"), header=FALSE,)
popNames <- as.matrix(popNames)
name <- NULL
table <- c(1:58)
for (table in popTables){
for (name in popNames){
pVals <- table$p
hist(pVals, breaks=20, xlab="P-val", main=name))
}
}
【问题讨论】:
-
因为你的循环中没有任何顺序发生,你一遍又一遍地调用同一个直方图。
标签: r for-loop graph histogram nested-loops