Vincy Pyne
2011-Mar-11 15:51 UTC
[R] Generation of random numbers in a function - (Return command)
Dear R helpers I have following data.frame and for each product_name, I have associated mean and standard deviation. I need to generate 1000 random no.s for each of these products and find the respective mean and standard deviation. My R code is as follows. library(plyr) library(reshape2) filtered_new <- data.frame(product_name = c("P1", "P2", "P3", "P4", "P5"), output_avg = c(22.71078,22.16979,21.34420,20.17421,19.83799), output_stdev = c(23.59924,21.21430,22.01025,18.88877,18.80436)) n <- 100 myfunction_mc = function(product_name, output_avg, output_stdev) { product_usage_borrowing_room_mc = rnorm(n, output_avg, output_stdev) output_avg_mc mean(product_usage_borrowing_room_mc) output_stdev_mc = sd(product_usage_borrowing_room_mc) return(output_avg_mc ) } result <- dlply(.data = filtered_new, .variables = "product_name", .fun = function(x) myfunction_mc(product_name = x$product_name, output_avg = x$output_avg, output_stdev = x$output_stdev)) result1 <- data.frame(result) result2 <- melt(result1) result <- data.frame(product = filtered_new$product_name, Monte_Carlo_result = result2$value) And it gives me the desired result. ######### PROBLEM is as given below - But if in the "myfunction_mc", in the return statement if I try to add 'output_stdev_mc' i.e. myfunction_mc = function(product_name, output_avg, output_stdev) { product_usage_borrowing_room_mc = rnorm(n, output_avg, output_stdev) output_avg_mc = mean(product_usage_borrowing_room_mc) output_stdev_mc = sd(product_usage_borrowing_room_mc) return(output_avg_mc, output_stdev_mc) # I have added output_stdev_m } result <- dlply(.data = filtered_new, .variables = "product_name", .fun = function(x) myfunction_mc(product_name = x$product_name, output_avg = x$output_avg, output_stdev = x$output_stdev)) I get following error - Error in return(output_avg_mc, output_stdev_mc) : multi-argument returns are not permitted Kindly guide. Regards Vincy [[alternative HTML version deleted]]
jim holtman
2011-Mar-11 17:05 UTC
[R] Generation of random numbers in a function - (Return command)
You can only return a single object. You might want to create a list if you have multiple objects: return(list(output_avg_mc, output_stdev_mc)) On Fri, Mar 11, 2011 at 10:51 AM, Vincy Pyne <vincy_pyne at yahoo.ca> wrote:> Dear R helpers > > I have following data.frame and for each product_name, I have associated mean and standard deviation. I need to generate 1000 random no.s for each of these products and find the respective mean and standard deviation. > > My R code is as follows. > > > library(plyr) > library(reshape2) > > filtered_new <- data.frame(product_name = c("P1", "P2", "P3", "P4", "P5"), > output_avg = c(22.71078,22.16979,21.34420,20.17421,19.83799), > output_stdev = c(23.59924,21.21430,22.01025,18.88877,18.80436)) > > n <- 100 > > myfunction_mc = function(product_name, output_avg, output_stdev) > > { > > product_usage_borrowing_room_mc = rnorm(n, output_avg, output_stdev) > > output_avg_mc > ?mean(product_usage_borrowing_room_mc) > output_stdev_mc = sd(product_usage_borrowing_room_mc) > > return(output_avg_mc ) > > } > > result <- dlply(.data = filtered_new, .variables = "product_name", .fun = function(x) > ???????????????? myfunction_mc(product_name = x$product_name, output_avg = x$output_avg, > ?? ??? ??? ??? ? output_stdev = x$output_stdev)) > > result1 <- data.frame(result) > > result2 <- melt(result1) > > result <- data.frame(product = filtered_new$product_name, Monte_Carlo_result = result2$value) > > And it gives me the desired result. > > > ######### PROBLEM is as given below - > > But if in the "myfunction_mc", in the return statement if I try to add 'output_stdev_mc' > ?i.e. > > myfunction_mc = function(product_name, output_avg, output_stdev) > > { > > product_usage_borrowing_room_mc = rnorm(n, output_avg, output_stdev) > > output_avg_mc = mean(product_usage_borrowing_room_mc) > output_stdev_mc = sd(product_usage_borrowing_room_mc) > > return(output_avg_mc, output_stdev_mc)??????????? # I have added output_stdev_m > > } > > result <- dlply(.data = filtered_new, .variables = "product_name", .fun = function(x) > ???????????????? myfunction_mc(product_name = x$product_name, output_avg = x$output_avg, > ?? ??? ??? ??? ? output_stdev = x$output_stdev)) > > I get following error - > > Error in return(output_avg_mc, output_stdev_mc) : multi-argument returns are not permitted > > Kindly > ?guide. > > Regards > > > Vincy > > > ? ? ? ?[[alternative HTML version deleted]] > > > ______________________________________________ > R-help at 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. > >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?