Maithili Shiva
2009-Dec-17 06:35 UTC
[R] CORRECTION - Generation of Random numbers in a loop
Dear R helpers, please ignore my earlier mail. Here is the corrected mail.
Please forgive me for the lapses on my part. Extremely sorry.
Here is the corrected mail.
Dear R helpers
I am having following data
Name Numbers
A11 12
A12 17
A13 0
A11 11
A12 6
A13 0
A11 8
A12 4
A13 3
CONDITIONS
If Name is A11, min_val = 1.05, max_val = 1.30
If Name is A12, min_val = 1.30, max_val = 1.60
If Name is A13, min_val = 1.60, max_val = 1.99
TASK
To generate the Uniform random nos for each of these Names (Equal to the
corresponding no. e.g. the 5th Name is A12, so I need to generate 6 uniform
random numbers in the range (1.30 - 1.99). Also I need to arrange these random
numbers one by one in a single csv file i.e. say 12 random numbers in teh range
(1.05-1.30) followed by 17 random numbers in the range (1.30-1.60) and so on.
# ___________________________________________________
Here is the R code I have tried
ONS <- read.table(textConnection("name number
A11 12
A12 17
A13 0
A11 11
A12 6
A13 0
A11 8
A12 4
A13 3"), header = TRUE)
X = as.character(ONS$name)
Y = ONS$number
Z = NULL
for (i in 1:length(X))
{
if(X[i] == 'A11')
{
min_val = 1.05
max_val = 1.30
Z = runif(Y[i], min_val, max_val)
}
else
{
if(X[i] == 'A12')
{
min_val = 1.30
max_val = 1.60
Z = runif(Y[i], min_val, max_val)
}
else
{
if(X[i] == 'A13')
{
min_val = 1.60
max_val = 1.99
Z = runif(Y[i], min_val, max_val)
}
}
}
}
# End of Code
## _____________________________________________________________________
PROBLEM
I need to get 61 random numbers which is total of all the numbers (1st 12 fo A,
3 random numbers for B, 13 for C, 5 again fo A and so on). The result whcih I
got is
> Z
[1] 1.740443 1.761758 1.797222
which is pertaining to the last name C where 3 random numbers are generated i.e.
Z instaed of getting added, is overwritten.
Please help me to rectify my code so that in the end I will get 61 random
numbers as desired i.e. 12 for A in the range (1.05 - 1.30), 3 for B in the
range (1.30 - 1.60), 13 for C in the range (1.60-1.99), again 5 for A in the
range (1.05 - 1.30).
Thanking in advance. I also sincerely apologize for writing such a long mail, as
I wanted to be clear as possible in my communication.
Regards
Maithili
The INTERNET now has a personality. YOURS! See your Yahoo! Homepage.
[[alternative HTML version deleted]]
Gray Calhoun
2009-Dec-17 11:27 UTC
[R] CORRECTION - Generation of Random numbers in a loop
Hi Maithili,
This is probably easiest if you think in terms of functions. Just
apply 'runif' along the right vectors
## gives you one data frame with the min and max
limits <- read.table(textConnection("name min max
A11 1.05 1.30
A12 1.30 1.60
A13 1.60 1.99"), header = TRUE)
d <- merge(ONS, limits)
cbind(randomdraw = unlist(mapply(runif, d$number, d$min, d$max)), name
= rep(d$name, d$number))
## now save it to a csv file, etc.
On Wed, Dec 16, 2009 at 10:35 PM, Maithili Shiva
<maithili_shiva at yahoo.com> wrote:> Dear R helpers, please ignore my earlier mail. Here is the corrected mail.
Please forgive me for the lapses on my part. Extremely sorry.
>
> Here is the corrected mail.
>
>
> Dear R helpers
>
> I am having following data
>
> Name?????????? Numbers
> A11??????????????????12
> A12??????????????????17
> A13?????????????????? 0
> A11??????????????????11
> A12?????????????????? 6
> A13?????????????????? 0
> A11?????????????????? 8
> A12?????????????????? 4
> A13???????????????????3
>
> CONDITIONS
>
> If Name is A11, min_val = 1.05, max_val = 1.30
> If Name is A12, min_val = 1.30, max_val = 1.60
> If Name is A13, min_val = 1.60, max_val = 1.99
>
> TASK
>
> To generate the?Uniform random nos for each of these Names?(Equal to the
corresponding no. e.g. the 5th Name is A12, so I need to generate 6 uniform
random numbers in the range (1.30 - 1.99). Also I need to arrange these random
numbers?one by one in a single csv file i.e. say?12 random numbers in teh range
(1.05-1.30) followed by?17 random numbers in the range (1.30-1.60) and so on.
>
> # ___________________________________________________
>
> Here is the R code I have tried
>
> ONS <- read.table(textConnection("name number
> A11??? 12
> A12??? 17
> A13???? 0
> A11??? 11
> A12????? 6
> A13????? 0
> A11????? 8
> A12????? 4
> A13????? 3"), header = TRUE)
>
> X = as.character(ONS$name)
> Y = ONS$number
>
> Z = NULL
>
> for (i in 1:length(X))
> ?? {
> ?? if(X[i] == 'A11')
> ?? {
> ?? min_val = 1.05
> ?? max_val = 1.30
> ?? Z = runif(Y[i], min_val, max_val)
> ?? }
> ?? else
> ?? {
> ?? if(X[i] == 'A12')
> ?? {
> ?? min_val = 1.30
> ?? max_val = 1.60
> ?? Z = runif(Y[i], min_val, max_val)
> ?? }
> ?? else
> ?? {
> ?? if(X[i] == 'A13')
> ?? {
> ?? min_val = 1.60
> ?? max_val = 1.99
> ?? Z = runif(Y[i], min_val, max_val)
> ?? }
> ?? }
> ?? }
> ?? }
>
> # End of Code
>
> ## _____________________________________________________________________
>
> PROBLEM
>
> I need to get 61 random numbers which is total of all the numbers?(1st 12
fo A, 3 random numbers for B, 13 for C, 5 again fo A and so on). The result
whcih I got is
>
>> Z
> [1] 1.740443 1.761758 1.797222
>
> which is pertaining to the last name C where 3 random numbers are generated
i.e. Z instaed of getting added, is overwritten.
>
> Please help me to rectify my code so that in the end I will get 61 random
numbers as desired i.e. 12 for A in the range (1.05 - 1.30), 3 for B in the
range (1.30 - 1.60), 13 for C in the range (1.60-1.99), again 5 for A in the
range (1.05 - 1.30).
>
> Thanking in advance. I also sincerely apologize for writing such a long
mail, as I wanted to be clear as possible in my communication.
>
> Regards
>
> Maithili
>
>
> ? ? ?The INTERNET now has a personality. YOURS! See your Yahoo! Homepage.
> ? ? ? ?[[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.
>
>
--
Gray Calhoun
Assistant Professor of Economics
Iowa State University