[R] How to append the random no.s for different variables in the same data.frame

PIKAL Petr petr.pikal at precheza.cz
Wed Sep 12 09:24:48 CEST 2012


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 like

Why 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]]




More information about the R-help mailing list