For your problem A, why do you want to create so many variables?  Leave the
data in the 'newrate' list and reference the information from there.  It
will be much easier.  Think about the data structures that you want to work
with, especially if you have a variable number of objects/files that you are
going to be processing.  It is not entirely clear what you are trying to do,
but from the basic structure it does appear that with the right data
structure you can do what you want without having to 'replicate' a lot of
code.  For example, if you can easily reference the items in 'newrate' as
the following:

newrate[[1]]$min1
newrate[[2]]$max3    # and so on

This can be done in a loop and it appears that most of Problem B can be
handled by common code.  You probably need to read a little more about
'lists' because they are your friend in these kind of situations.

On Sun, Dec 27, 2009 at 10:55 PM, Maithili Shiva
<maithili_shiva@yahoo.com>wrote:

>
> Dear R helpers,
>
> I have following input files. (Actually they are more than 10 rates but
> here i am considering only 2 rates to write my problem)
>
> rate1.csv
> min1        max1            min2          max2          min3           max3
> 1.05        1.30               1.30          1.65             1.65
> 1.99
>
> rate2.csv
> min1        max1            min2          max2          min3           max3
> 2.05        2.30               2.30          2.65             2.65
> 2.99
>
>
> The simple R code I had used to read these files was as given below -
>
> # OLD CODE
>
> rates1 = read.csv('rate1.csv)
> rates2 = read.csv('rate2.csv')
>
> rate1_min1  = rates1$min1
> rate1_max1 = rates1$max1
> rate1_min2  = rates1$min2
> rate1_max2 = rates1$max2
> rate1_min3  = rates1$min3
> rate1_max3 = rates1$max3
>
> rate2_min1  = rates2$min1
> rate2_max1 = rates2$max1
> rate2_min2  = rates2$min2
> rate2_max2 = rates2$max2
> rate2_min3  = rates2$min3
> rate2_max3 = rates2$max3
>
>
> Since I am not aware how many rates I will be dealing with (it can be 2, 4,
> or 10), hence I can't hardcode my input like I had defined above.
>
> So with the guidance of R - help, I had rerwitten the input code as given
> below.
>
> # NEW CODE
>
> n = 2  # It gives me no of rates
>
> X = paste('rate', 1:n, '.csv', sep = ' ' )
>
> # the output is "rate1.csv"  " rate2.csv"
>
> newrate = lapply(X, read.csv)
>
> # the output is as given below
>
> newrate
>
> [ [1] ]
>         min1         max1        min2      max2     min3       max3
> 1       1.05        1.30          1.30        1.65       1.65        1.99
>
> [ [2] ]
>         min1         max1        min2      max2     min3       max3
> 1       2.05        2.30          2.30        2.65       2.65        2.99
>
> ## _________________________________________________
>
> ## PROBLEM
>
> # PROBLEM - A
>
> If I apply the command (As given in OLD CODE above)
>
> rate1_min1  = rates1$min1
> # The output which I get is
>     min1
> 1  1.05
>
> On similar lines I define
>
> rate1_min1 = newrate[[1]] [1]
> # The output which I get is
>     min1
> 1  1.05
>
> rate1_max1 = newrate[[1]] [2]
>
> will give me output
>
>     max1
> 1  1.30
>
> and so on.
>
> So I will have to define it 12 times to assign the respective min and max
> values.
>
> My problem is instead of hard coding like I have done in OLD CODE, I need
> to assign these respective input values using a loop or some other way (as
> it becomes cumbersome if no of rates exceed say 10 and also since I am not
> aware how many rate files i will be dealing with) so that in the end I
> should be able to assign (say) the values like
>
> rate1_min1  = 1.05
> rate1_max1 = 1.30
> rate1_min2  = 1.30
> rate1_max2 = 1.65
> rate1_min3  = 1.65
> rate1_max3 = 1.99
>
> rate2_min1  = 2.05
> rate2_max1 = 2.30
> rate2_min2  = 2.30
> rate2_max2 = 2.65
> rate2_min3  = 2.65
> rate2_max3 = 2.99
>
> If there are say 10 rates, then this will continue till
>
> rate10_min1 =  ......
> rate10_max1 = ......
> .........
> so on.
>
> ## ________________________________________________________
>
> # PROBLEM - B
>
> # Suppose Rij = ith Rate and jth range. (There are 3 ranges i.e. j= 3).
>
> data_label = expand.grid(c("R11", "R12", "R13"), c("R21", "R23", "R23"))
>
> # gives the output like
>
>   data_label
>      Var1        Var2
> 1    R11         R21
> 2    R12         R21
> 3    R13         R21
> 4    R11         R22
> 5    R12         R22
> 6    R13         R22
> 7    R11         R23
> 8    R12         R23
> 9    R13         R23
>
>
> If instead of two rates, suppose there are three rates, I will have to
> modify my code as
>
> data_label = expand.grid(c("R11", "R12", "R13"), c("R21", "R23", "R23"),
> c("R31", "R32", "R33"))
>
> If I define say
>
> n = no_of_rates  # I will be taking this figure from some otehr csv file.
>
> My PROBLEM = B is how do I write the above code pertaining to data_label in
> terms of 'n'.
>
> ## _______________________________________________________________________
>
> I understand I am giving the impression as I need some kind of spoon
> feeding. I am giving below the original complete OLD CODE (consisting of 4
> rates) I had written in the beginning. Problem with me is to modify it for
> varaible number of rates.
>
> I sincerely apologize for the inconvenience I have caused so far by asking
> queries. Please guide me.
>
> Regards
>
> Maithili
>
> # ____________________________________________
>
> MY ORIGINAL CODE (Actual HARD CODE)
>
> ## ONS - PPA
>
> # INPUT
>
> rate_1   = read.csv('rate1_range.csv')
> rate_2   = read.csv('rate2_range.csv')
> rate_3   = read.csv('rate3_range.csv')
> rate_4   = read.csv('rate4_range.csv')
>
> prob_1   = read.csv('rate1_probs.csv')
> prob_2   = read.csv('rate2_probs.csv')
> prob_3   = read.csv('rate3_probs.csv')
> prob_4   = read.csv('rate4_probs.csv')
>
> rate1_min_1   =  rate_1$rate1_range1_min
> rate1_max_1  =  rate_1$rate1_range1_max
> rate1_min_2   =  rate_1$rate1_range2_min
> rate1_max_2  =  rate_1$rate1_range2_max
> rate1_min_3   =  rate_1$rate1_range3_min
> rate1_max_3  =  rate_1$rate1_range3_max
>
> rate2_min_1   =  rate_2$rate2_range1_min
> rate2_max_1  =  rate_2$rate2_range1_max
> rate2_min_2   =  rate_2$rate2_range2_min
> rate2_max_2  =  rate_2$rate2_range2_max
> rate2_min_3   =  rate_2$rate2_range3_min
> rate2_max_3  =  rate_2$rate2_range3_max
>
> rate3_min_1   =  rate_3$rate3_range1_min
> rate3_max_1  =  rate_3$rate3_range1_max
> rate3_min_2   =  rate_3$rate3_range2_min
> rate3_max_2  =  rate_3$rate3_range2_max
> rate3_min_3   =  rate_3$rate3_range3_min
> rate3_max_3  =  rate_3$rate3_range3_max
>
> rate4_min_1   =  rate_4$rate4_range1_min
> rate4_max_1  =  rate_4$rate4_range1_max
> rate4_min_2   =  rate_4$rate4_range2_min
> rate4_max_2  =  rate_4$rate4_range2_max
> rate4_min_3   =  rate_4$rate4_range3_min
> rate4_max_3  =  rate_4$rate4_range3_max
>
>
> ## _______________________________________________________________________________________________________________________________________________________________________________________________
>
> ## Rij : ith rate jth range i.e. R23 represents 3rd Range of Rate 2
> data_lab <- expand.grid(c("R11", "R12", "R13"), c("R21", "R22", "R23"),
> c("R31", "R32", "R33"), c("R41", "R42", "R43"))
>
> range_prob <- list()
>
> range_prob[[1]]   <- c(prob_1$rate1_prob1,prob_1$rate1_prob2,
> prob_1$rate1_prob3)
> range_prob[[2]]   <- c(prob_2$rate2_prob1,prob_2$rate2_prob2,
> prob_2$rate2_prob3)
> range_prob[[3]]   <- c(prob_3$rate3_prob1,prob_3$rate3_prob2,
> prob_3$rate3_prob3)
> range_prob[[4]]   <- c(prob_4$rate4_prob1,prob_4$rate4_prob2,
> prob_4$rate4_prob3)
>
> ## COMPUTATIONS
>
> pdf <- expand.grid(range_prob)
> data_lab$probs <- apply(pdf, 1, prod)
> joint_probs = xtabs(probs ~ Var1 + Var2+Var3+Var4, data = data_lab)
> write.csv(data.frame(joint_probs), 'joint_probs.csv', row.names = FALSE)
>
>
> #________________________________________________________________________________________________________________________________________________________________________________________________________________________
>
> ONS = read.csv('joint_probs.csv')
>
> Names <- apply(ONS[, -5], 1, paste, collapse = ' ')
> write.csv(data.frame(rate_combination = Names, Probability = ONS[, 5]),
> 'prob_table.csv', row.names = FALSE)
> #
> ______________________________________________________________________________________________________________________________________________________________________________________________________________________________
> N = 10000 # No of random numbers to be generated for each of the 4 rates
> no_of_instances = round(N*(read.csv('prob_table.csv')$Probability))
>
>
> # #  THE ACTUAL PROBLEM - TO GENERATE 10000 random numbers in proportion
> and depending on the range of teh given rate in teh combination
>
> write.csv(data.frame(rate1_range = ONS[,1], rate2_range = ONS[,2],
> rate3_range = ONS[,3], rate4_range = ONS[,4], no_of_instances), 'Final
> Table.csv', row.names = FALSE)
>
>
> ##  Random number generation for Rate1
> ## -----------------------------------
>
> HM = read.csv('Final Table.csv')
> rate1_rates=rep(c("R11","R12","R13"),27)
> rate1_rates
> number = HM$no_of_instances
> rate1_combination=rep(rate1_rates,number)
> rate1_combination
>
> interval.min=ifelse(rate1_combination=="R11",1.05,ifelse(rate1_combination=="R12",1.3,1.6))
>
> interval.max=ifelse(rate1_combination=="R11",1.3,ifelse(rate1_combination=="R12",1.6,1.99))
> rand.nums_rate1=runif(rate1_combination,min=interval.min,max=interval.max)
> rate1_series = data.frame(rate1_combination,rand.nums_rate1)
> write.csv(data.frame(rate1_series), 'rate1_series.csv', row.names = FALSE)
>
>
> ## Random number generation for Rate2
>
> rate2_rates=rep(c("R21","R21","R21","R22","R22","R22","R23","R23","R23"),9)
> rate2_rates
> number = HM$no_of_instances
> rate2_combination=rep(rate2_rates,number)
> rate2_combination
>
> interval.min=ifelse(rate2_combination=="R21",2.05,ifelse(rate2_combination=="R22",2.3,2.6))
>
> interval.max=ifelse(rate2_combination=="R21",2.3,ifelse(rate2_combination=="R22",2.6,2.99))
> rand.nums_rate2=runif(rate2_combination,min=interval.min,max=interval.max)
> rate2_series = data.frame(rate2_combination,rand.nums_rate2)
> write.csv(data.frame(rate2_series), 'rate2_series.csv', row.names = FALSE)
>
>
> ## Random number generation for Rate3
>
>
> rate3_rates=rep(c("R31","R31","R31","R31","R31","R31","R31","R31","R31","R32","R32","R32","R32","R32","R32","R32","R32","R32","R33","R33","R33","R33","R33","R33","R33","R33","R33"),3)
> rate3_rates
> number = HM$no_of_instances
> rate3_combination=rep(rate3_rates,number)
> rate3_combination
>
> interval.min=ifelse(rate3_combination=="R31",3.05,ifelse(rate3_combination=="R32",3.3,3.6))
>
> interval.max=ifelse(rate3_combination=="R21",3.3,ifelse(rate3_combination=="R32",3.6,3.99))
> rand.nums_rate3=runif(rate3_combination,min=interval.min,max=interval.max)
> rate3_series = data.frame(rate3_combination,rand.nums_rate3)
> write.csv(data.frame(rate3_series), 'rate3_series.csv', row.names = FALSE)
>
>
> ## Random number generation for Rate4
>
> rate4_rates=rep(c("R41","R42","R43"), each = 27)
> rate4_rates
> number = HM$no_of_instances
> rate4_combination=rep(rate4_rates,number)
> rate4_combination
>
> interval.min=ifelse(rate4_combination=="R41",4.05,ifelse(rate4_combination=="R42",4.3,4.6))
>
> interval.max=ifelse(rate4_combination=="R41",4.3,ifelse(rate4_combination=="R42",4.6,4.99))
> rand.nums_rate4=runif(rate4_combination,min=interval.min,max=interval.max)
> rate4_series = data.frame(rate4_combination,rand.nums_rate4)
> write.csv(data.frame(rate4_series), 'rate4_series.csv', row.names = FALSE)
>
> ##  Generation of combination names as per the no of random numbers
> generated
>
> PP = read.csv('prob_table.csv')$rate_combination
> PPP = rep(PP, number)
>
> write.csv(data.frame(rate_combination = PPP, rate1 =
> round(read.csv('rate1_series.csv')$rand.nums, digits = 4), rate2 =
> round(read.csv('rate2_series.csv')$rand.nums, digits = 4), rate3 =
> round(read.csv('rate3_series.csv')$rand.nums, digits = 4), rate4 =
> round(read.csv('rate4_series.csv')$rand.nums, digits = 4)),
> 'Main_Random_Numbers.csv', row.names = FALSE)
>
>
> _________________________________________________________________________________________________________________________
> ## END of code : ONS-PPA
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> [[elided Yahoo spam]]
>
>        [[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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

	[[alternative HTML version deleted]]


