0

I have a series of iterations that I am trying to run inside of a function.

This should take the values from one df price, cost and find the price value inside of df2 and pull quantity....then price should be multiplied by a list seq(1,10,by=1) and the resulting quantity should be pulled from df2 and be multiplied by cost from df1. The df's are like so:

> head(df1)
   NormP    NormQ NormProf      cost         prod      oldp      oldq   oldprof
1:  12.6 3.508948 43.79424 0.1192674 PRODUCT15765 1.0084482 0.2517925 0.9853869
2:  12.6 3.508948 43.69372 0.1479132 PRODUCT41810 0.9758223 0.3044252 1.0361177
3:  12.6 3.508948 43.70965 0.1433742 PRODUCT16363 0.9699599 0.1110663 1.0135204
4:  12.6 3.508948 43.53103 0.1942769 PRODUCT45158 0.8990833 0.5590313 1.0062068
5:  12.6 3.508948 43.63993 0.1632420 PRODUCT42031 0.9724028 0.2305520 1.0306521
6:  12.6 3.508948 43.68393 0.1507020 PRODUCT15729 0.9894182 0.0638638 0.9869835

And

> head(dem1)
  NormP    NormQ
1   0.0 6.768876
2   0.1 6.030973
3   0.2 7.371158
4   0.3 4.792864
5   0.4 6.202993
6   0.5 4.194690

The function runs without error, but the resulting list comes back empty. Why?

Here is the code:

percents <- function(df1, df2) {

  # Create an empty list to store results
  l1 <- list(rep(0, 10))

  # Iterate through old prices
  iter <- 1
  for(n in 1:nrow(df1)){
    cat("Running prod iteration ",iter, "\n")

    # Create a vector for price changes 
    pct_change <- seq(1, 10, by = .1)
    len <- length(pct_change)

    # Empty list to store results 
    l2 <- list(rep(0, len))

    # Get the cost
    cost <- df1[n, "cost"]

    # Select the old price, quantity, and profit
    oldp <- df1[n, "oldp"]
    oldq <- df1[n, "oldq"]
    oldprof <- df1[n, "oldprof"]

    # Select the new prices
    iter <- 1
    for(i in pct_change) {
      cat("Running % iteration ",iter, "\n")

      thisPct <- i

      cost <- df1[n, "cost"]

      oldp <- df1[n, "oldp"]
      oldq <- df1[n, "oldq"]
      oldprof <- df1[n, "oldprof"]

      # New price
      newp <- df1[n, "oldp"] * thisPct
      newp <- round(newp, 1)
      cat("Selected PCT", "\n")

      # New quantity
      newq <- df2[df2$NormP == newp, "normq"] 
      cat("Selected Q", "\n")

      # New profit
      newprof <- (newp - cost) * newq
      cat("Selected Prof", "\n")

      out <- list(cost = cost, oldp = oldp, oldq = oldq, oldprof = oldprof, newq = newq, newp = newp, newprof = newprof)

      l2[[iter]] <- out

      iter <- iter + 1
    }

    cat("Combining PCT results", "\n")

    l1[[iter]] <- l2

    iter <- iter + 1
  }

  cat("Combining Prod results", "\n")

  return(l1)
} 
zsad512
  • 861
  • 3
  • 15
  • 41
  • 1
    It would be easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and the desired output so we can run and test the code. – MrFlick Nov 22 '17 at 19:53
  • 1
    For one thing, you reset `iter` to 1 and then increment its length(seq(1, 10, by = .1)) times in the inner loop every time. As a result, you are assigning to the same element of l1 (`l1[[iter]]`) every time. Thanks for adding the code...but as @MrFlick stated, a [self-contained, reproducible example](https://www.tidyverse.org/help/#reprex) would be ideal. – Eric Nov 22 '17 at 20:22
  • @MrFlick i added some sample input :) – zsad512 Nov 22 '17 at 20:37
  • That data is not in a reproducible format which does not make it easy to work with. This line looks problematic: `newq <- df2[df2$NormP == newp, "normq"] `. Trying to do [equality tests with decimal values is not a good idea](https://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal). – MrFlick Nov 22 '17 at 20:42

0 Answers0