3

I have an input data set, which may look something like this:

DF=data.frame(
    Variable = c("Test1", "Test2", "Test3"), 
    Distribution = c("Normal", "Exponential","Poisson"),
    Variable = c(2, 3, 4), 
    SD = c(2, NA, NA))

I want to use the random probability functions (e.g. rnorm rexp and rbinom) using the distributions given in the data frame DF.

So, how do I turn the text input into the correct functions?

I want to use the corresponding values in the Variable and SD columns as the mean values/standard deviations if appropriate.

2
  • 1
    Those functions have different input arguments that your DF doesn't have (eg., size, n). How do you think dealing with that? Commented Mar 14, 2018 at 11:21
  • It was just an example - the only missing parameter is n which for arguments sake say will be 10 for all distributions. The Variable column will be the mean for the Normal distribution, rate for Exponential, and lambda for Poisson. Should have everything. Commented Mar 14, 2018 at 11:23

2 Answers 2

2

@r.user.05apr solution is working, but involves some expression parsing which is not needed here. We might get this a lot easier by creating a list of functions to use them later.

# generating data:
DF=data.frame(
  Variable = c("Test1", "Test2", "Test3"), 
  Distribution = c("Normal", "Exponential","Poisson"),
  VariablePrm = c(2, 3, 4), 
  SD = c(2, NA, NA),
  stringsAsFactors = FALSE)

# creating function list and selecting this functions by Distribution column
fun_vec <- c(Normal=rnorm, Exponential=rexp, Poisson=rpois)
DF$fun <- fun_vec[DF$Distribution]

# if SD is NA then simply call function only with variablePrm
# else call with sd
# 10 is the number of observations to generate
generate <- function(x) {
  if(is.na(x$SD)){
    x$fun(10, x$VariablePrm)
  }else{
    x$fun(10, x$VariablePrm, x$SD)
  }
}
# if we apply this functions to each row we will get matrix of results
# each column will have 10 rows of generated data for previously selected distribution
apply(DF, 1, generate)
Sign up to request clarification or add additional context in comments.

Comments

0

Something like:

DF=data.frame(
  Variable = c("Test1", "Test2", "Test3"), 
  Distribution = c("Normal", "Exponential","Poisson"),
  VariablePrm = c(2, 3, 4), 
  SD = c(2, NA, NA),
  stringsAsFactors = FALSE)

# functions-lookup
fun_vec <- c("rnorm", "rexp", "rpois")
names(fun_vec) <- c("Normal", "Exponential", "Poisson")
DF$fun <- fun_vec[DF$Distribution]

# create expr
my_expr <- function(x) {
  txt <- paste0(x[1], "<-", x[5], "(", 10, ", ", x[3], 
                ifelse(is.na(x[4]), "", 
                       paste0(", ", x[4])), ")")
}
want <- apply(DF, 1, function(x) eval(parse(text = my_expr(x))))
colnames(want) <- DF$Variable
want

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.