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

时间:2017-06-14 12:59:33

标签: r loops for-loop combinations

我正在尝试在三个字符向量中使用多个元素的组合,但我只获得了最后一个元素迭代,我还想设置一个条件,我的 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 = "")
         }
     }
    }
    }

我期待像

这样的输出
[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
  

注意:在列表第一个元素的输出中只包含 BudgetMax   参数AND列表的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"

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

5 个答案:

答案 0 :(得分: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])

你也得到了理想的结果:

> 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"

说明:

  • 使用expand.grid,您可以创建向量bhkbudg_minbudg_max的所有组合。
  • 由于因子变量budg_minbudg_max的级别是货币值的递增顺序,因此您可以通过将这些因子转换为整数来过滤掉budg_min > budg_max的情况。 / LI>
  • 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])

答案 1 :(得分:4)

如果您使用apply,则可以划掉循环。

expand.grid为您提供三个向量的所有组合:

allcombs <-expand.grid(bhk = bhk,bmin = budg_min, bmax =budg_max) 

您创建的索引budg_min小于或等于budg_max

ix <- apply(allcombs,1,function(x) which(budg_min %in% x[2]) <= which(budg_max %in% x[3]))

然后只根据你的条件形成真实的组合:

res <- apply(allcombs[ix,],1,function(x) paste(text1,x[1],text3,text4,text5,x[2],text6,x[3]))

答案 2 :(得分:2)

每次创建新的k元素时,您都会覆盖uuu [i]的值。

例如第一次循环 i == 1,j == 1,k == 1 然后,将您的第一个值分配给uuu [1] 第二次贯穿 i == 1,j == 1,k == 2 然后你还将你的第二个值分配给uuu [1](因为我仍然等于1)

要解决此问题,您需要一个单独的计数器来跟踪列表中的项目。

这样的事情应该会给你一个新元素的每一个组合。

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")
item <- 1

for(i in bhk){
  for(j in budg_min){
    # Split budg_min to separate value from unit
    min <- unlist(strsplit(j,"-"))
    # Convert Crores to Lacs to get min value in Lacs
    min <- ifelse(min[2] == "Crores", as.numeric(min[1]) * 100, as.numeric(min[1]))

    for(k in budg_max) {
      # Split budg_min to separate value from unit
      max <- unlist(strsplit(k,"-"))
      # Convert Crores to Lacs to get max value in Lacx
      max <- ifelse(max[2] == "Crores", as.numeric(max[1]) * 100, as.numeric(max[1]))

      # If min is less than max, insert the comparison
      if(min < max){
        uuu[item] <- paste(text1,i,text3,text4,text5,j,text6,k,sep = "")
        item <- item + 1
      }
    }
  }
}

答案 3 :(得分:1)

似乎uuu [i]是造成问题的原因。试试这个:

df <- data.frame()
for(i in bhk){
  for(j in budg_min){
    for(k in budg_max) {
      uuu=data.frame(paste(text1,i,text3,text4,text5,j,text6,k,sep = ""))
      df <- rbind(df, uuu)
    }
  }
}

答案 4 :(得分:0)

考虑使用多个args进行sapply / vapply:

mat <- sapply(budg_min, function(j, k, i, t1, t3, t4, t5, t6) 
  paste0(t1,i,t3,t4,t5,j,t6,k), budg_max, bhk, text1, text3, text4, text5, text6)

mat <- vapply(budg_min, function(j, k, i, t1, t3, t4, t5, t6) 
  paste0(t1,i,t3,t4,t5,j,t6,k), character(25), USE.NAMES = TRUE, budg_max, bhk, text1, text3, text4, text5, text6)

上述任何一个的返回都是命名列矩阵(来自第一个输入列表的名称或 budg_min )。但是你想要使用长字符向量as.vector(mat)

enter image description here