gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:34:04 24 4
gpt4 key购买 nike

我正在尝试对三字符向量中的多个元素使用组合,但我只得到最后一个元素迭代,我还想提出一个条件,即我的 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

note:in the output above 1st element of list contains only BudgetMax parameter AND last(nth) element of list has only BudgetMax parameter and rest elements are the combination of bhk , budg_min and 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"

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

最佳答案

无需使用 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您创建向量的所有组合 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])

关于r - 使用 Multiple for 循环获取所有元素的组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44545547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com