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 sincerely apologize for the inconvenience I have caused so far by asking queries. Please guide me. Regards Maithili
The INTERNET now has a personality. YOURS! See your Yahoo! Homepage. [[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.