On Wed, Sep 12, 2012 at 7:51 AM, Vincy Pyne <vincy_p...@yahoo.ca> wrote: > Dear R helpers, > > (At the outset I sincerely apologize if I have not put forward my following > query properly, though I have tried to do so.) > > > Following is a curtailed part of my R - code where I am trying to generate > say 100 random no.s for each of the products under consideration. > > > 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)) > > > There are some 12 products (and this may vary each time). In my original > code, the "return" statement returns me some other output. Here for > simplicity sake, I am just using the values as given in input. > > > PROBLEM - A : > > I want to store the random no.s (BUR_mc) as generated above for each of the > products and store them in a single data.frame. Now when I access > 'sim_BUR.csv', I get the csv file where the random nos. generated for the > last product are getting stored. I need something like > > product random no > product1 ....... > product1 ....... > ............................. > > product1 ....... # (This is 100th value > generated for product1) > product2 ....... > product2 ....... > > ............................ > ............................ > ............................ > > Problem - B > > Also, is it possible to have more than one 'return' statements in a given > function?
No: if you want to return multiple values, put them in a list and return that: f <- function(n, nn){ x <- rnorm(n) y <- rexp(nn) ret <- list(x = x, y = y) return(ret) } Cheers, M > > Thanking in advance > > Vincy > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.