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_p...@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]]

______________________________________________
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.

Reply via email to