循环遍历大型列表以查找特定值

时间:2015-06-05 19:05:22

标签: r list for-loop

我有一个记事本文件列出了美国的所有邮政编码,我想查找特定的邮政编码(由用户指定),在该邮政编码上运行一些代码,打印出"The temperature at [input] is 60"这样的语句我想要为我的zipcode档案中出现的指定邮政编码和接下来的10个连续邮政编码执行此操作。我的第一个问题是,我不知道如何将包含所有这些数字的zipcode文档转换为列表或数组,或者任何可以轻松运行for循环的文件。我非常陌生,所以请耐心等待。

Input = "20904"# User provides an input for this
    ZipData<-read.csv(file.path(wd,"DataImport","zip_code_data.csv"),
                      colClasses=c("character","NULL","factor","NULL","NULL","factor",
                                   "NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL",
                                   "NULL","NULL"),
                      col.names=c("zip","","city","","","state","","","","","","","","",
                                  "",""))
    ZipData<- as.numeric(ZipData$zip)
edit(ZipData) # Opens up a notepad document listing the zipcodes starting with "c(544, 601, 602, 603, 604, 605, 606, 610, 611, 612, 613, 614....)
# Note: typeof(ZipData) prints out "double"

    # Bunch of code that ends with:
    a <- paste("The current temperature for", cityName, Input, "is", temperature, sep=" ")
    print(a)

我想在Input和接下来的10个zipcodes上运行它。我在制定循环遍历整个列表的for循环时遇到问题(我不知道ZipData是否可以归类为列表),找到用户指定的zipcode,在其上运行我的代码块并冲洗和重复对于接下来的10个邮政编码。我的程序应以10个打印语句结束,列出所有温度。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

这可能是您所指的:

axes.imshow()