【问题标题】:Loop through a large list to look for certain values循环遍历一个大列表以查找某些值
【发布时间】:2015-06-05 19:05:21
【问题描述】:

我有一个记事本文档,列出了美国的所有邮政编码,我想查找一个特定的邮政编码(由用户指定),在该邮政编码上运行一些代码,打印出像 @987654321 这样的语句@我想做对于指定的邮政编码和出现在我的邮政编码档案中的接下来的 10 个连续的邮政编码。我的第一个问题是我不知道如何将包含所有这些数字的邮政编码文档转换为列表或数组,或者任何可以轻松运行 for 循环的东西。我对 R 非常陌生,所以请耐心等待。

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 个邮政编码上运行它。我无法制定一个循环遍历整个列表的 for 循环(我不知道 ZipData 是否可以归类为列表),找到用户指定的邮政编码,在其上运行我的代码块并冲洗并重复接下来的 10 个邮政编码。我的程序应该以列出所有温度的 10 个打印语句结束。有什么想法吗?

【问题讨论】:

  • 信息不足。您提到“当前”值,但您的数据是否包含时间维度并且我们报告的是最新值?你询问接下来的 10 个邮政编码,但没有定义它。它是(排序的)数据中的下一个十个吗?它是来自用户的一系列输入吗?无论如何,我怀疑match 函数可能会有所帮助,例如match(Input,ZipData$zip) 会很快为您提供正确的行。
  • @Frank 邮编都是按顺序排列的。因此,只需获取输入邮政编码之后出现在列表(?)中的接下来的 10 个元素。我的问题是首先尝试找到 Input 元素的索引。

标签: r list for-loop


【解决方案1】:

这可能是您所指的:

zip_plus10 <- function(input) {
  index <- which(zip.vector == as.numeric(input))
  paste('The current temperature for',
        city[index:(index+10)],
        state[index:(index+10)],
        zip.vector[index:(index+10)],
        'is: ',
        temps[index:(index+10)]
        )
}

zip_plus10('90210')

 [1] "The current temperature for K HI 90210 is:  65" 
 [2] "The current temperature for L ID 90211 is:  66" 
 [3] "The current temperature for M IL 90212 is:  58" 
 [4] "The current temperature for N IN 90213 is:  110"
 [5] "The current temperature for O IA 90214 is:  57" 
 [6] "The current temperature for P KS 90215 is:  91" 
 [7] "The current temperature for Q KY 90216 is:  90" 
 [8] "The current temperature for R LA 90217 is:  89" 
 [9] "The current temperature for S ME 90218 is:  108"
[10] "The current temperature for T MD 90219 is:  109"
[11] "The current temperature for U MA 90220 is:  55" 

#Data
    set.seed(444)
zip.vector <- seq(90200, 90221)
city <- LETTERS[1:length(zip.vector)]
state <- state.abb[1:length(zip.vector)]
temps <- sample(50:110, length(zip.vector))

【讨论】:

  • +1 用于提供样本数据(正如 OP 可以说应该有的那样)。我认为match 比== 更好,因为它会在找到匹配项时停止。此外,要镜像 OP,您可能需要添加 ZipData &lt;- data.frame(zip.vector,city,state,temps),然后在 with(ZipData,{}) 中执行操作。最后,我认为cat 可能比paste 更好地向用户显示结果。
  • @Frank cat 可以进行一些操作。我也喜欢match。谢谢你。顺便说一句,当你超过 1,000 次代表的门槛时,你会得到一件绿色夹克吗:)
  • 那是高尔夫参考吗?恭喜你通过了那个门槛:)
  • 是的。我想必须要有一个数字徽章。
猜你喜欢
  • 1970-01-01
  • 2022-08-17
  • 2021-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多