我正在尝试生成一个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"
有人能帮我看看我做错了吗?有用帖子的链接也会有所帮助。
答案 0 :(得分:3)
您可以使用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)
来消除循环,这会扩展两个向量的所有组合:
pip
答案 1 :(得分:3)
循环不起作用,因为第一个循环URLlist [a]的迭代的每个元素被覆盖68 * 34次,并且在每次结束时只有最后一个组合,即York 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
}}