Vincy Pyne
2012-Sep-12 06:51 UTC
[R] How to append the random no.s for different variables in the same data.frame
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? Thanking in advance Vincy [[alternative HTML version deleted]]
R. Michael Weylandt
2012-Sep-12 07:14 UTC
[R] How to append the random no.s for different variables in the same data.frame
On Wed, Sep 12, 2012 at 7:51 AM, Vincy Pyne <vincy_pyne at 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 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. >
PIKAL Petr
2012-Sep-12 07:24 UTC
[R] How to append the random no.s for different variables in the same data.frame
Hi> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Vincy Pyne > Sent: Wednesday, September 12, 2012 8:51 AM > To: r-help at r-project.org > Subject: [R] How to append the random no.s for different variables in > the same data.frame > > 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 likeWhy do you want to write csv files? Maybe it would be better to form this data frame directly. As you did not provide any date here are some fake mydf<-expand.grid(1:10,letters[1:3]) mydf$rnum<-rnorm(30) mydf Var1 Var2 rnum 1 1 a 0.35928359 2 2 a 0.27431573 3 3 a 0.22948381 4 4 a -1.31041870 5 5 a 2.57832871 6 6 a 0.10697714 .................... 29 9 c -0.33768297 30 10 c 0.85797343 To add mean and sd mydf$m<-ave(dmydf$rnum, dmydf$Var2, FUN=mean) dmydf$sd<-ave(dmydf$rnum, dmydf$Var2, FUN=sd)> > 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?AFAIK no. But you can store output values in list/vector/matrix/data frame so you can get more results in one return statement. Regards Petr> > Thanking in advance > > Vincy > > [[alternative HTML version deleted]]
Vincy Pyne
2012-Sep-12 07:36 UTC
[R] How to append the random no.s for different variables in the same data.frame
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)) 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 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 ....... # (There will be 100 such values for product1) product2 ....... product2 ....... ............................ ............................ product12 ...... ............................ product12 ....... Thanking you in advance Vincy [[alternative HTML version deleted]]
PIKAL Petr
2012-Sep-12 08:49 UTC
[R] How to append the random no.s for different variables in the same data.frame
Hi It is good to cc to R help. It would be even better if you provide toy data to illustrate what do you have and what do you want. Sometimes you will find solution when preparing such data yourself without need for posting to the rhelp. I am still not sure what do you really want to achieve so below are some points From: Vincy Pyne [mailto:vincy_pyne@yahoo.ca] Sent: Wednesday, September 12, 2012 10:09 AM To: PIKAL Petr Subject: How to append the random no.s for different variables in the same data.frame Dear Mr Petr Sir, I sincerely apologize to you for taking the liberty of writing to you. I thank you for your valuable solution you have given. However, I am missing some point and request you to please guide me if possible. I am giving my original R code below. # ..................................................................................... library(plyr) ead_mc = function(product, output_avg, output_stdev) { product_usage_borrowing_room_mc = rnorm(n, output_avg, output_stdev) Here you create random numbers with some mean and sd # I need to store these random no.s for all products in a single data frame. product_usage_borrowing_room_mc_filtered1 <- subset(product_usage_borrowing_room_mc, product_usage_borrowing_room_mc > 0) product_usage_borrowing_room_mc_filtered <- subset(product_usage_borrowing_room_mc_filtered1, product_usage_borrowing_room_mc_filtered1 <= 100) Here you drop values over 100 and below 0 Why don't you use runif(n, 0,100)? Anyway, I still am rather in doubt what do you want to achieve further. Without knowing how your data look like it is really difficult to provide some help. What was wrong on the code I suggested to you? Here is some modification based on information snippets from your mail. mydf <-data.frame(product = rep(letters[1:3], each=10)) mydf$monte<-1 mydf$monte<-unlist(lapply(split(mydf$monte, mydf$product), function(x) rnorm(x, 10, 10))) selection <- mydf$monte>0&mydf$monte<=100 mydf_filt<-mydf[selection, ] mydf_filt$m<-ave(mydf_filt$monte, mydf_filt$product, FUN=mean) mydf_filt$sd<-ave(mydf_filt$monte, mydf_filt$product, FUN=sd) head(mydf_filt) product monte m sd 1 a 3.978948 12.83564 7.240065 2 a 16.889179 12.83564 7.240065 3 a 13.062089 12.83564 7.240065 4 a 12.458633 12.83564 7.240065 5 a 23.495197 12.83564 7.240065 7 a 3.291643 12.83564 7.240065 Gives you one data frame with column of random numbers between 0 and 100 and column of means and sd for each product. Is this what do you want? If not, please elaborate on such toy data what do you have and what do you want as a result. Regards Petr selection <- product_usage_borrowing_room_mc > 0 & product_usage_borrowing_room_mc <= 100 product_usage_borrowing_room_mc_filtered <- product_usage_borrowing_room_mc[selection , ] output_avg_mc = mean(product_usage_borrowing_room_mc_filtered) output_stdev_mc = sd(product_usage_borrowing_room_mc_filtered) product_usage_borrowing_room_mc_filtered_sorted = sort(product_usage_borrowing_room_mc_filtered, decreasing = FALSE) ead_monte_carlo = product_usage_borrowing_room_mc_filtered_sorted[alpha*length(product_usage_borrowing_room_mc_filtered_sorted)] return(list(output_avg_mc, output_stdev_mc, ead_monte_carlo)) } result <- dlply(.data = filtered_new, .variables = "product", .fun = function(x) ead_mc(product = x$product, output_avg = x$output_avg, output_stdev = x$output_stdev)) # End of Code # --------------------------------------------------------------- In the first line of function, I have used product_usage_borrowing_room_mc = rnorm(n, output_avg, output_stdev) So when for a given product this loop (i.e. function) is run, a new set of random no.s is stored. My return statement gives me a different required output which is my main requirement. Simply put, when the loop is run, I need to store the random no.s in a single data.frame for all products. Sir, I once again apologize for taking this liberty of writing to you. The attached zip file gives the R code and related input files if in case you will like to see. Thanking you and will certainly appreciate if you kindly guide me. With warm regards Vincy [[alternative HTML version deleted]]
PIKAL Petr
2012-Sep-12 09:01 UTC
[R] How to append the random no.s for different variables in the same data.frame
Hi This is the error I get with your code and this prevents us to give you reasonable help. Error in eval.quoted(.variables, data) : object 'My_data' not found Petr> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Vincy Pyne > Sent: Wednesday, September 12, 2012 9:36 AM > To: R. Michael Weylandt > Cc: r-help at r-project.org > Subject: Re: [R] How to append the random no.s for different variables > in the same data.frame > > 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)) > > > 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 > > 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???????? .......????????????????? # (There will be 100 such > values for product1) > product2???????? ....... > product2???????? ....... > > ............................ > ............................ > product12?????? ...... > ............................ > product12?????? ....... > > > Thanking you in advance > > Vincy > > > [[alternative HTML version deleted]]
PIKAL Petr
2012-Sep-12 09:03 UTC
[R] How to append the random no.s for different variables in the same data.frame
Hi and please do not use HTML messages, they get scrambled during processing. Plain text is preferred. Regards Petr> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of PIKAL Petr > Sent: Wednesday, September 12, 2012 10:49 AM > To: Vincy Pyne > Cc: r-help > Subject: Re: [R] How to append the random no.s for different variables > in the same data.frame > > Hi > > It is good to cc to R help. It would be even better if you provide toy > data to illustrate what do you have and what do you want. Sometimes you > will find solution when preparing such data yourself without need for > posting to the rhelp. > > I am still not sure what do you really want to achieve so below are > some points > > > > From: Vincy Pyne [mailto:vincy_pyne at yahoo.ca] > Sent: Wednesday, September 12, 2012 10:09 AM > To: PIKAL Petr > Subject: How to append the random no.s for different variables in the > same data.frame > > > Dear Mr Petr Sir, > > I sincerely apologize to you for taking the liberty of writing to you. > I thank you for your valuable solution you have given. However, I am > missing some point and request you to please guide me if possible. I am > giving my original R code below. > > # > ....................................................................... > .............. > > library(plyr) > > ead_mc = function(product, output_avg, output_stdev) > > > { > > product_usage_borrowing_room_mc = rnorm(n, output_avg, output_stdev) > > Here you create random numbers with some mean and sd # I need to store > these random no.s for all products in a single data frame. > > product_usage_borrowing_room_mc_filtered1 <- > subset(product_usage_borrowing_room_mc, product_usage_borrowing_room_mc > > 0) > > product_usage_borrowing_room_mc_filtered <- > subset(product_usage_borrowing_room_mc_filtered1, > product_usage_borrowing_room_mc_filtered1 <= 100) > > Here you drop values over 100 and below 0 Why don't you use runif(n, > 0,100)? > Anyway, I still am rather in doubt what do you want to achieve further. > Without knowing how your data look like it is really difficult to > provide some help. > What was wrong on the code I suggested to you? Here is some > modification based on information snippets from your mail. > mydf <-data.frame(product = rep(letters[1:3], each=10)) > mydf$monte<-1 > mydf$monte<-unlist(lapply(split(mydf$monte, mydf$product), function(x) > rnorm(x, 10, 10))) selection <- mydf$monte>0&mydf$monte<=100 > mydf_filt<-mydf[selection, ] mydf_filt$m<-ave(mydf_filt$monte, > mydf_filt$product, FUN=mean) mydf_filt$sd<-ave(mydf_filt$monte, > mydf_filt$product, FUN=sd) > head(mydf_filt) > product monte m sd > 1 a 3.978948 12.83564 7.240065 > 2 a 16.889179 12.83564 7.240065 > 3 a 13.062089 12.83564 7.240065 > 4 a 12.458633 12.83564 7.240065 > 5 a 23.495197 12.83564 7.240065 > 7 a 3.291643 12.83564 7.240065 > Gives you one data frame with column of random numbers between 0 and > 100 and column of means and sd for each product. > Is this what do you want? If not, please elaborate on such toy data > what do you have and what do you want as a result. > Regards > Petr > selection <- product_usage_borrowing_room_mc > 0 & > product_usage_borrowing_room_mc <= 100 > product_usage_borrowing_room_mc_filtered <- > product_usage_borrowing_room_mc[selection , ] output_avg_mc > mean(product_usage_borrowing_room_mc_filtered) > output_stdev_mc = sd(product_usage_borrowing_room_mc_filtered) > > product_usage_borrowing_room_mc_filtered_sorted > sort(product_usage_borrowing_room_mc_filtered, decreasing = FALSE) > > ead_monte_carlo > product_usage_borrowing_room_mc_filtered_sorted[alpha*length(product_us > age_borrowing_room_mc_filtered_sorted)] > > return(list(output_avg_mc, output_stdev_mc, ead_monte_carlo)) > > } > > result <- dlply(.data = filtered_new, .variables = "product", .fun > function(x) > ead_mc(product = x$product, output_avg = x$output_avg, > output_stdev = x$output_stdev)) > > > > # End of Code > > # --------------------------------------------------------------- > > In the first line of function, I have used > > product_usage_borrowing_room_mc = rnorm(n, output_avg, output_stdev) > > So when for a given product this loop (i.e. function) is run, a new set > of random no.s is stored. > > My return statement gives me a different required output which is my > main requirement. > > Simply put, when the loop is run, I need to store the random no.s in a > single data.frame for all products. > > Sir, I once again apologize for taking this liberty of writing to you. > The attached zip file gives the R code and related input files if in > case you will like to see. > > Thanking you and will certainly appreciate if you kindly guide me. > > With warm 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.