0

Having trouble. What I have is:

dec_amt <- function(x, k) format(round(x, k), nsmall=k)      # Formatting decimal places 

example.df <- data.frame(replicate(8,sample(0:100000,1000,rep=TRUE)))
names(example.df) <- c("AF", "CD", "CS", "ED", "LP", "PI", "RR", "TD")

probTab_test2 <- function(x = c(...), y=c(...), z=c(...))

{ 
  m.TABLE <- list()
  EXP <- list()
  PROB <- c(seq(.10, .90, .10), seq(.91,.99,.01), seq(.995, .999, .001))
  PERIOD <- dec_amt(1/(1-PROB), 2)

  for (i in 1:(length(z)))  { 

    if (length(z) == 1) 
    { 
      break 
    } 

    EXP <- quantile(example.df[,z[i]], PROB)
    EXP <- formatC(EXP, format='d', big.mark=',')
    m.TABLE <- list(data.frame(PERIOD, EXP))

    print(m.TABLE)
  }

  EXP <- quantile(example.df[,z], PROB)
  EXP <- formatC(EXP, format='d', big.mark=',')

  TABLE <- data.frame(PERIOD, EXP)

  return(TABLE)
}

probTab_test2(c("Consumer Products"), c("All Revenues"),c("TD", "LP"))

 Error in `[.data.frame`(x, order(x, na.last = na.last, decreasing = decreasing)) : 
  undefined columns selected 

What I want is, if the length of the argument z is > 1 then for every element of z, I want it to create an 'EXP' column that I can bind into a dataframe (defined as m.TABLE) so at the end I would have a list of 'z' number of dataframes.

I feel like the quantile function is not happy about me passing through a dataframe instead of a vector, but not sure how to get around that in this loop. Suggestions would be great, happy to provide further information.

Note - feel free to disregard my x, y arguments - those will be used to call an outside function, but not noteworthy for this issue.

2
  • quantile() is not for dataframes. library("fortunes"); fortune(85) Commented Jun 18, 2017 at 17:52
  • Right, is there any way around this issue? Can I somehow extract each specified element of 'z' column from the example.df issue as a vector and pass it through quantile()? Right now, I'm just grabbing it as a dataframe. Commented Jun 18, 2017 at 17:55

1 Answer 1

1

You can make use of the lapply function to loop over the variables listed in z and simplify the code:

probTab_test2 <- function(x = c(...), y=c(...), z=c(...)) { 
    PROB <- c(seq(.10, .90, .10), seq(.91,.99,.01), seq(.995, .999, .001))
    PERIOD <- dec_amt(1/(1-PROB), 2)
    m.TABLE <- lapply(as.data.frame(example.df[, z]),  function (vector) {
        quantiles <- quantile(vector, PROB)
        formatted.quantiles <- formatC(EXP, quantiles, format='d', big.mark=',')
        return(data.frame(PROB, formatted.quantiles))
  })
    return(m.TABLE)
}

For each variable listed in z, this function calculates the quantiles and creates a data frame for each variable listed in z. The as.data.frame is necessary so that lapply works even when length(z)==1.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.