R 替代嵌套 for 循环以创建 URL 列表 (expand.grid)

R Alternative to nested for loop to create a list of URLs (expand.grid)

我正在尝试生成 URL 的列表,其中包含以下两个列表:

County<-list("ADAMS", "ALLEGHENY", "ARMSTRONG", "BEAVER", "BEDFORD", "BERKS", "BLAIR", "BRADFORD", "BUCKS", "BUTLER", "CAMBRIA", "CAMERON", "CARBON", "CENTRE", "CHESTER", "CLARION", "CLEARFIELD", "CLINTON", "COLUMBIA", "CRAWFORD", "CUMBERLAND", "DAUPHIN", "DELAWARE", "ELK", "ERIE", "FAYETTE", "FOREST", "FRANKLIN", "FULTON", "GREENE", "HUNTINGDON", "INDIANA", "JEFFERSON", "JUNIATA", "LACKAWANNA", "LANCASTER", "LAWRENCE", "LEBANON", "LEHIGH", "LUZERNE", "LYCOMING", "MCKEAN", "MERCER", "MIFFLIN", "MONROE", "MONTGOMERY", "MONTOUR", "NORTHAMPTON", "NORTHUMBERLAND", "PERRY", "PHILADELPHIA", "PIKE", "POTTER", "SCHUYLKILL", "SNYDER", "SOMERSET", "STATE LEVEL SITES", "SULLIVAN", "SUSQUEHANNA", "TIOGA", "UNION", "VENANGO", "WARREN", "WASHINGTON", "WAYNE", "WESTMORELAND", "WYOMING", "YORK")

RepPeriod<-list ("15AUGU","15JULU","15JUNU","15MAYU","15APRU", "15MARU", "15FEBU", "15JANU", "2015-1", "2014-2","2014-1","2014-0", "2013-2","2013-1","2013-0", "2012-2","2012-1","2012-0","2011-2","2011-1","2011-0", "2010-3","2010-2","2010-0", "2009-0","2008-0","2007-0", "2006-0","2005-0","2004-0","2003-0","2002-0","2001-0","2000-0")

总共将是一个包含 2312 个元素的列表(68 个国家/地区* 34 个报告期)

我试过这个:

URLlist<-as.character(c(1:2312))


for (a in 1:2312){
        for (i in 1:length(RepPeriod)){
                for (j in 1:length(County)){
         URLlist[a]<-paste0("https://www.paoilandgasreporting.state.pa.us/publicreports/Modules/Production/ProductionByCountyExport.aspx?UNCONVENTIONAL_ONLY=false&INC_HOME_USE_WELLS=true&INC_NON_PRODUCING_WELLS=true&PERIOD=",RepPeriod[i],"&COUNTY=",County[j])
                }
        }
}

它只是粘贴了最后一个报告期和县 2312 次,而不是生成排列:

URLlist[1:3]
[1] "https://www.paoilandgasreporting.state.pa.us/publicreports/Modules/Production/ProductionByCountyExport.aspx?UNCONVENTIONAL_ONLY=false&INC_HOME_USE_WELLS=true&INC_NON_PRODUCING_WELLS=true&PERIOD=2000-0&COUNTY=YORK"
[2] "https://www.paoilandgasreporting.state.pa.us/publicreports/Modules/Production/ProductionByCountyExport.aspx?UNCONVENTIONAL_ONLY=false&INC_HOME_USE_WELLS=true&INC_NON_PRODUCING_WELLS=true&PERIOD=2000-0&COUNTY=YORK"
[3] "https://www.paoilandgasreporting.state.pa.us/publicreports/Modules/Production/ProductionByCountyExport.aspx?UNCONVENTIONAL_ONLY=false&INC_HOME_USE_WELLS=true&INC_NON_PRODUCING_WELLS=true&PERIOD=2000-0&COUNTY=YORK"

谁能帮我看看我做错了什么?指向有用帖子的链接也会有所帮助。

您可以使用 expand.grid 消除循环,它扩展了两个向量的所有组合:

z <- expand.grid(RepPeriod, County)

URLlist <- paste0("https://www.paoilandgasreporting.state.pa.us/publicreports/Modules/Production/ProductionByCountyExport.aspx?UNCONVENTIONAL_ONLY=false&INC_HOME_USE_WELLS=true&INC_NON_PRODUCING_WELLS=true&PERIOD=",z$Var1,"&COUNTY=",z$Var2) 

循环不工作,因为第一个循环 URLlist[a] 迭代的每个元素都被覆盖 68*34 次,并且在每次结束时只有最后一个组合,即约克 a 和 2000-0被储存了。你必须在循环中间有一个增量计数器来避免这种情况,比如这个循环:

{k = 0
for (i in 1:length(RepPeriod)){
for (j in 1:length(County)){
  URLlist[j+k]<-paste0("........",RepPeriod[i],"&COUNTY=",County[j])
}
k = k + 68
}}