使用Multiple for循环来获取所有元素的组合

dee*_*esh 2 combinations loops for-loop r

我正在尝试在三个字符向量中使用多个元素的组合,但我只获得最后一个元素迭代,我还想在创建组合列表时设置我的budg_min不应大于budg_max的条件

这是我的代码

    text1="http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom="
    text3="&proptype="
    text4="Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment"
    text5="&cityName=Thane&BudgetMin="
    text6="&BudgetMax="


    uuu=list()
    bhk=c("1","2","3","4","5",">5")
    budg_min=c("5-Lacs","10-Lacs","20-Lacs","30-Lacs","40-Lacs","50-Lacs","60-Lacs","70-Lacs","80-Lacs","90-Lacs","1-Crores","1.2-Crores","1.4-Crores","1.6-Crores","1.8-Crores","2-Crores","2.3-Crores","2.6-Crores","3-Crores","3.5-Crores","4-Crores","4.5-Crores","5-Crores","10-Crores","20-Crores")
    budg_max=c("5-Lacs","10-Lacs","20-Lacs","30-Lacs","40-Lacs","50-Lacs","60-Lacs","70-Lacs","80-Lacs","90-Lacs","1-Crores","1.2-Crores","1.4-Crores","1.6-Crores","1.8-Crores","2-Crores","2.3-Crores","2.6-Crores","3-Crores","3.5-Crores","4-Crores","4.5-Crores","5-Crores","10-Crores","20-Crores")

    for(i in bhk){
      for(j in budg_min){
        for(k in budg_max) {
      if(budg_min>budg_max){"Skip that combination "}
else{


     uuu[i]=paste(text1,i,text3,text4,text5,j,text6,k,sep = "")
         }
     }
    }
    }
Run Code Online (Sandbox Code Playgroud)

我期待输出像

[1]
http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=1&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMax=5-Lacs
[2]
http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=1&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=5-Lacs&BudgetMax=10-Lacs
[3]
http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=1&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=5-Lacs&BudgetMax=20-Lacs
.
.
.
.
[n]
http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=%3E5&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMax=20-Crores
Run Code Online (Sandbox Code Playgroud)

注意:在上面的输出中,list的第一个元素只包含BudgetMax 参数,list的last(nth)元素只有BudgetMax参数,rest元素是bhk,budg_min和budg_min的组合.

但是我的代码只提供了6条记录

[1] "http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=1&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=20-Crores&BudgetMax=20-Crores"

$`2`
[1] "http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=2&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=20-Crores&BudgetMax=20-Crores"

$`3`
[1] "http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=3&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=20-Crores&BudgetMax=20-Crores"

$`4`
[1] "http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=4&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=20-Crores&BudgetMax=20-Crores"

$`5`
[1] "http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=5&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=20-Crores&BudgetMax=20-Crores"

$`>5`
[1] "http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=>5&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=20-Crores&BudgetMax=20-Crores"
Run Code Online (Sandbox Code Playgroud)

应该对我的代码进行哪些更改,以便它提供我的所有组合.任何帮助将不胜感激.谢谢

Jaa*_*aap 7

无需使用for循环.用expand.gridsprintf:

eg <- expand.grid(bhk = bhk, budg_min = budg_min, budg_max = budg_max)
eg <- eg[as.integer(eg$budg_min) <= as.integer(eg$budg_max),]
uuu <- sprintf("%s%s%s%s%s%s%s%s", text1,eg[,1],text3,text4,text5,eg[,2],text6,eg[,3])
Run Code Online (Sandbox Code Playgroud)

你也得到了理想的结果:

> head(uuu,10)
 [1] "http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=1&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=5-Lacs&BudgetMax=5-Lacs"
 [2] "http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=2&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=5-Lacs&BudgetMax=5-Lacs"
 [3] "http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=3&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=5-Lacs&BudgetMax=5-Lacs"
 [4] "http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=4&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=5-Lacs&BudgetMax=5-Lacs"
 [5] "http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=5&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=5-Lacs&BudgetMax=5-Lacs"
 [6] "http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=>5&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=5-Lacs&BudgetMax=5-Lacs"
 [7] "http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=1&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=5-Lacs&BudgetMax=10-Lacs"
 [8] "http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=2&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=5-Lacs&BudgetMax=10-Lacs"
 [9] "http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=3&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=5-Lacs&BudgetMax=10-Lacs"
[10] "http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=4&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Thane&BudgetMin=5-Lacs&BudgetMax=10-Lacs"
Run Code Online (Sandbox Code Playgroud)

说明:

  • 随着expand.grid你创建矢量的所有组合bhk,budg_minbudg_max.
  • 由于因子变量的级别budg_minbudg_max货币值的递增顺序,您可以过滤掉budg_min > budg_max通过将这些因子转换为整数的情况.
  • sprintf根据指定的格式("%s%s%s%s%s%s%s%s")将所有向量粘贴在一起.%s格式的每个部分都由向量的元素替换.

要将所有货币值转换为lacs,您可以(受@MattJewett启发):

eg <- expand.grid(bhk = bhk, budg_min = budg_min, budg_max = budg_max) 

# Convert values to lacs prior to min/max comparison
eg$min_lacs <- as.numeric(gsub('([0-9.]+).*','\\1',eg$budg_min))
eg$min_lacs[grepl('Crores',eg$budg_min)] <- eg$min_lacs[grepl('Crores',eg$budg_min)]*100
eg$max_lacs <- as.numeric(gsub('([0-9.]+).*','\\1',eg$budg_min))
eg$max_lacs[grepl('Crores',eg$budg_max)] <- eg$max_lacs[grepl('Crores',eg$budg_max)]*100

eg <- eg[as.integer(eg$min_lacs) <= as.integer(eg$max_lacs),]
uuu <- sprintf("%s%s%s%s%s%s%s%s", text1,eg[,1],text3,text4,text5,eg[,2],text6,eg[,3])
Run Code Online (Sandbox Code Playgroud)