Reputation: 516
AIM: I would like to add a "distribution" and "parameters" to the summary
output of a function I created.
The function I created takes a n
and a choice of distribution and returns random numbers from that distribution. The output of the function is an object of the class MyClass
.
Further below I created the summary
function for objects of MyClass
which currently returns the length, mean and standard deviation of the output.
PROBLEM: I would like for the summary
function to also print out the original parameters of the random_number_generator
function, see DESIRED OUTPUT below.
random_number_generator <- function(n,distribution,mean=NULL,sd=NULL,lambda=NULL,size=NULL,prob=NULL){
if (distribution=="Normal"){
x <- rnorm(n=n,mean=mean,sd=sd)
}
if (distribution=="Poisson"){
x <- rpois(n=n, lambda=lambda)
}
if (distribution=="Binomial") {
x <- rbinom(n=n,size=size,prob=prob)
}
class(x) <- "MyClass"
return (x)
}
answer <- random_number_generator(n=10, distribution = "Binomial",size=15,prob=0.4)
summary.MyClass <- function(x) {
stopifnot(inherits(x, "MyClass"))
cat("\t\n",
sprintf("Sample size: %s\n", length(x)),
sprintf("Mean: %s\n", mean(x)),
sprintf("Standard Deviation: %s\n", sd(x))
)
}
CURRENT OUTPUT:
summary(answer)
Sample size: 10
Mean: 4.9
Standard Deviation: 1.59513148186739
DESIRED OUTPUT:
class(answer)
[1] "MyClass"
summary(answer)
Sample size: 10
Mean: 4.9
Standard Deviation: 1.59513148186739
Distribution: "Binomial"
Size: 15
Prob: 0.4
Upvotes: 1
Views: 42
Reputation: 1234
You could also store your parameters as attributes of your return value. That way you don't get a list back if you don't want that:
random_number_generator <- function(n,distribution,mean=NULL,sd=NULL,lambda=NULL,size=NULL,prob=NULL){
if (distribution=="Normal"){
x <- rnorm(n=n,mean=mean,sd=sd)
}
if (distribution=="Poisson"){
x <- rpois(n=n, lambda=lambda)
}
if (distribution=="Binomial") {
x <- rbinom(n=n,size=size,prob=prob)
}
attributes(x) <- list(distribution = distribution,
size = size,
prob = prob)
class(x) <- "MyClass"
return (x)
}
answer <- random_number_generator(n=10, distribution = "Binomial",size=15,prob=0.4)
summary.MyClass <- function(x) {
stopifnot(inherits(x, "MyClass"))
cat("\t\n",
sprintf("Sample size: %s\n", length(x)),
sprintf("Mean: %s\n", mean(x)),
sprintf("Standard Deviation: %s\n", sd(x)),
sprintf("Distribution: %s\n", attr(x, "distribution")),
sprintf("Size: %s\n", attr(x, "size")),
sprintf("Prob: %s\n", attr(x, "prob"))
)
}
summary(answer)
Upvotes: 1
Reputation: 48201
We could do the following:
random_number_generator <- function(n, distribution, mean = NULL, sd = NULL, lambda = NULL, size = NULL, prob = NULL) {
if (distribution == "Normal")
x <- rnorm(n = n, mean = mean, sd = sd)
else if (distribution == "Poisson")
x <- rpois(n = n, lambda = lambda)
else if (distribution == "Binomial")
x <- rbinom(n = n, size = size, prob = prob)
out <- list(x = x, distribution = distribution, size = size, prob = prob)
class(out) <- "MyClass"
out
}
summary.MyClass <- function(obj) {
stopifnot(inherits(obj, "MyClass"))
cat("\t\n",
sprintf("Sample size: %s\n", length(obj$x)),
sprintf("Mean: %s\n", mean(obj$x)),
sprintf("Standard Deviation: %s\n", sd(obj$x)),
sprintf("Distribution: %s\n", obj$distribution),
sprintf("Size: %d\n", obj$size),
sprintf("Probability: %s\n", obj$prob)
)
}
answer <- random_number_generator(n = 10, distribution = "Binomial", size = 15, prob = 0.4)
summary(answer)
#
# Sample size: 10
# Mean: 5.5
# Standard Deviation: 1.64991582276861
# Distribution: Binomial
# Size: 15
# Probability: 0.4
So, to return extra information, we need to keep this extra information as an output from random_number_generator
, which I did with
out <- list(x = x, distribution = distribution, size = size, prob = prob)
class(out) <- "MyClass"
Upvotes: 2