On Wed, Sep 12, 2012 at 8:36 AM, Vincy Pyne <vincy_pyne@yahoo.ca> wrote:

>  Dear Mr Weylandt and R helpers,
>
> Thanks a lot for your suggestion. Unfortunately the "return" statement in
> my original R code returns me different results which are obtained after
> processing the function I have constructed.
>
> My requirement for storing the product-wise random numbers is just a part
> of my whole exercise. For each of the products, I generate a set of random
> no.s, process these, construct some statistics and obtain these statistics
> using the "Return" statement. So for each of the products, I get these set
> of statistics generated and that is not my problem.
>
> My problem is "BESIDES getting my required output (which anyways I am
> getting)", I need the product-wise random numbers I have already generated
> and store them together in a single data.frame. So a single data.frame
> gives me all the product wise random nos.
>
> I am reproducing my problem once again -
>
> # ____________________________________
>
>
>
> library(plyr)
> n = 100
>
> my_code = function(product, output_avg, output_stdev)
>
>     {
>
> BUR_mc = rnorm(n, output_avg, output_stdev)
>
> sim_BUR = data.frame(product, BUR_mc)
>
> write.csv(data.frame(sim_BUR), 'sim_BUR.csv', row.names = FALSE)
>
> return(list(output_avg, output_stdev))
>
>     }
>
>
> result <- dlply(.data = My_data, .variables = "product", .fun = function(x)
>                  my_code(product = x$product, output_avg = x$output_avg,
>                  output_stdev = x$output_stdev))
>
>
You're example isn't reproducible or to my mind particularly clear as to
what you're actually trying to do: do you have data that you want to
analyze or are you creating random data based on fixed parameters? If the
former, reproducible example: if the latter, why not generate all the data
sets, store them in a list, and then lapply()  your analysis to each set?
E.g.,

stdevs <- c(3,4,5)
means <- c(-1, 0, 1)

DATS <- mapply(function(m,s) rnorm(100, m,s), means, stdevs, SIMPLIFY =
FALSE) # Make data set
RES <- lapply(DATS, function(x) c(iqr = IQR(x), max = max(x), min = min(x)))

More generally: if you are going to want something (anything) after
function execution has ended, you must make provision to either 1) return
it or 2) recalculate it (perhaps by controlling the random seed)

Michael

	[[alternative HTML version deleted]]


