how to dedicate values to concerned loops

hey i want to pass different times eg interval= c(24,24,48,48,72,72,72,24,24,48) after passing time, concerned time should go to its loop(24 in first, 48 in second and 72 in third loop) and give output how i can do this please guide

if(interval+1<=24){
 print(" Urgent Package")   # Assume terrain factor is 1
 FPP=250                    # Assume fixed price for parcel is 250
 TC=100                     # per hr price for time
 fuelcost=117
 for(loc in 1:10){
   costofparceldelivery=250/10*fuelcost+FPP+TC*15/60*2
   print(paste("cost of delivery on loc",loc,"Rs",costofparceldelivery))}
 
}else if(interval+1<=48){
 print(" Premium Package")
 FPP=250                    # Assume fixed price for parcel is 250
 TC=100                     # per hr price for time
 fuelcost=117
 for(loc in 1:10){
   costofparceldelivery=250/10*fuelcost+FPP+TC*15/60*1.5
   print(paste("cost of delivery on loc",loc,"Rs",costofparceldelivery))}
 
}else if(interval+1<=72){
 print(" Economy Package")
 
 FPP=250                    # Assume fixed price for parcel is 250
 TC=100                     # per hr price for time
 fuelcost=117
 for(loc in 1:10){
   Dynamicpriceofloc = 250/10*fuelcost+FPP+TC*15/60
 print(paste("Platform Profit Rs",platformprofit))}
 
}else{
 print("Enter Valid Time")
}

your code for the last interval didnt make sense to me so I changed it so it operates on declared objects...
but here is a general approach

do_my_interval <- function(interval){
  if(interval+1<=24){
  print(" Urgent Package")   # Assume terrain factor is 1
  FPP=250                    # Assume fixed price for parcel is 250
  TC=100                     # per hr price for time
  fuelcost=117
  for(loc in 1:10){
    costofparceldelivery=250/10*fuelcost+FPP+TC*15/60*2
    print(paste("cost of delivery on loc",loc,"Rs",costofparceldelivery))}
  return(costofparceldelivery=costofparceldelivery)
}else if(interval+1<=48){
  print(" Premium Package")
  FPP=250                    # Assume fixed price for parcel is 250
  TC=100                     # per hr price for time
  fuelcost=117
  for(loc in 1:10){
    costofparceldelivery=250/10*fuelcost+FPP+TC*15/60*1.5
    print(paste("cost of delivery on loc",loc,"Rs",costofparceldelivery))}
  return(costofparceldelivery=costofparceldelivery)
}else if(interval+1<=72){
  print(" Economy Package")
  
  FPP=250                    # Assume fixed price for parcel is 250
  TC=100                     # per hr price for time
  fuelcost=117
  for(loc in 1:10){
    Dynamicpriceofloc = 250/10*fuelcost+FPP+TC*15/60
    print(paste("Dynamicpriceofloc ",Dynamicpriceofloc))}
  return(Dynamicpriceofloc=Dynamicpriceofloc)
}else{
  print("Enter Valid Time")
  return(NA)
}
}

interval= c(24,24,48,48,72,72,72,24,24,48)

my_results <- purrr::map(interval,
           do_my_interval)

my_results

This could be improved further by identifying the similarities among the intervals and further functionalising the process.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Despite my previous ability to help you, i managed to do it ... without understanding what you are doing.
Something about the code you shared involves distances ?

thanks it works , however what if in same interval 2 different distances have to be incorporated eg total 24hrs periods are 4 and each contain different distance then how i can get output please help

yes each time period(24 or 48 or 72 hrs) is having different distances i want to send reprex but its too long