[R] Random Number Generation in a Loop
Daniel Malter
daniel at umd.edu
Thu Dec 17 08:23:27 CET 2009
Hi, does this do what you want to do? I take the first six lines of the data
you provided. Note that "letters" corresponds to your "Name," and "number"
corresponds to your "Numbers" variable.
letters=rep(c("A","B","C"),2)
letters
number=c(25,3,13,5,7,0)
number
letters2=rep(letters,number)
letters2
interval.min=ifelse(letters2=="A",1.05,ifelse(letters2=="B",1.3,1.6))
interval.max=ifelse(letters2=="A",1.3,ifelse(letters2=="B",1.6,1.99))
rand.nums=runif(letters2,min=interval.min,max=interval.max)
data.frame(letters2,rand.nums)
HTH,
Daniel
-------------------------
cuncta stricte discussurus
-------------------------
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Maithili Shiva
Sent: Thursday, December 17, 2009 1:25 AM
To: r-help at r-project.org
Subject: [R] Random Number Generation in a Loop
Dear R helpers
I am having following data
Name Numbers
A 25
B 3
C 13
A 5
B 7
C 0
A 2
B 10
C 9
CONDITIONS
If Name is A, min_val = 1.05, max_val = 1.30 If Name is B, min_val = 1.30,
max_val = 1.60 If Name is C, 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 B, so I need to generate 7 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 25 random numbers in
teh range (1.05-1.30) followed by 3 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]]
More information about the R-help
mailing list