Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用“多个for循环”获取所有元素的组合_R_Loops_For Loop_Combinations - Fatal编程技术网

使用“多个for循环”获取所有元素的组合

使用“多个for循环”获取所有元素的组合,r,loops,for-loop,combinations,R,Loops,For Loop,Combinations,我试图在三字符向量中使用多个元素的组合,但我只得到最后一个元素迭代,我还想在创建组合列表时设置一个条件,即我的budg_最小值不应大于budg_最大值 这是我的密码 text1="http://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=" text3="&proptype=" text4="Multistorey-Apartment,Builder-Floor-Apa

我试图在三字符向量中使用多个元素的组合,但我只得到最后一个元素迭代,我还想在创建组合列表时设置一个条件,即我的budg_最小值不应大于budg_最大值

这是我的密码

    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
注意:在上述输出中,列表的第一个元素仅包含预算最大值 参数和列表的最后一个(n)元素只有BudgetMax参数 其余元素是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"
应该对我的代码进行哪些更改,以便它提供我的所有组合。
任何帮助都将不胜感激。谢谢

如果您使用
apply
,您可以从头开始循环

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

allcombs <-expand.grid(bhk = bhk,bmin = budg_min, bmax =budg_max) 
然后根据您的情况,仅形成正确的组合:

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

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

例如,第一次运行循环 i==1,j==1,k==1 然后将第一个值分配给uuu[1] 第二遍 i==1,j==1,k==2 然后还将第二个值赋给uuu[1](因为i仍然等于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
      }
    }
  }
}

text1似乎是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)
    }
  }
}

df无需用于循环。使用
expand.grid
sprintf

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])
说明:

  • 使用
    expand.grid
    可以创建向量
    bhk
    budg_min
    budg_max
    的所有组合
  • 由于因子变量
    budg_min
    budg_max
    的级别是按货币值的递增顺序排列的,因此可以通过将这些因子转换为整数筛选出
    budg_min>budg_max
    的情况
  • sprintf
    根据指定的格式将所有向量粘贴在一起(
    %s%s%s%s%s%s“
    )。格式的每个
    %s
    部分被向量的元素替换

要将所有货币值转换为LAC,您可以执行以下操作(受@MattJewett启发):


eg考虑使用多个参数的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)

mat您的代码运行良好,但请再次查看帖子,因为我也用了一些东西对其进行了编辑……谢谢您的努力!是的,这很有效。。这是今天的救世主…非常感谢你的努力@deepesh我现在也加入了一个转换是的。。。看到了更新,这真是太好了,集所有功能于一身。。。。再次感谢!!!:)在“df”中创建的组合包含一些不存在的url。因此,请使用其他条件编辑我的帖子……例如,如果您通过第26、51、52、76、77、78行中的数据框“df”,则包含budg_min大于budg_max的链接,理想情况下并非如此。所以对于这种url,它不会给出任何记录。因此,任何关于这一点的建议。谢谢你的努力!!!非常感谢你的努力。你的剧本写得很好,但我也在其他条件下编辑了这篇文章。谢谢你的努力!!!绝对@Val这在这种情况下非常有效…非常感谢您的努力!!!谢谢你的努力@Matt Jeweett。我已经编辑了一些其他条件以及文章。请再检查一遍。。谢谢你的努力!我已更新解决方案以处理最小/最大比较。其中还包含一些代码,可用于处理从crores到LaC的转换,从而对值进行更准确的比较。您可能希望找到一种方法将其合并到@Jaap提供的解决方案中,以确保将这些值作为建议编辑的相同货币单位.Thx进行比较。希望你不介意我拒绝了它,因为在我看来它可以做得更有效。请参阅我答案的更新。
> 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"
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])
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)