Dear R helpers, Suppose I am dealing with no of interest rates at a time and the no of interest rates I am selecting for my analysis is random i.e. it can be 2, can be 10 or even higher. The R-code I had written (with the guidance of R helpers obviously and I am really grateful to all of you) as of now is sort of hard coding when it comes to reading interest rates as an input e.g. ## 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') However, since I am not sure how many interest rates I will be dealing with to begin with, I have tried to call these inputs using a loop as follows. In my R working directory, following files are there which are to be read as input.. rate1_range.csv rate2_range.csv rate3_range.csv rate4_range.csv rate1_probs.csv rate2_probs.csv rate3_probs.csv rate4_probs.csv My R Code # _______________________________________________________________ n = no_of_Interest_Rates # This 'n' will be suppllied separately& can be anything. n= 4 # As mentioned, this input will be added seperately. for (i in 1:n) { rate_[i] = read.csv('rate[i]_range.csv') prob_[i] = read.csv('rate[i]_probs.csv') } # End of code. However when I excute this code (here, n = 4), I get following error. Error in file(file, "r") : cannot open the connection In addition: Warning message: In file(file, "r") : cannot open file 'rate[i]_range.csv': No such file or directory Obviously as usual I have written some stupid code. Please guide Regards Maithili The INTERNET now has a personality. YOURS! See your Yahoo! Homepage. [[alternative HTML version deleted]]
See the FAQs, particularly "How can I save the result of each iteration in a loop into a separate file?" which applies for the other way round as well. Uwe Ligges Maithili Shiva wrote:> Dear R helpers, > > Suppose I am dealing with no of interest rates at a time and the no of interest rates I am selecting for my analysis is random i.e. it can be 2, can be 10 or even higher. The R-code I had written (with the guidance of R helpers obviously and I am really grateful to all of you) as of now is sort of hard coding when it comes to reading interest rates as an input e.g. > > ## 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') > > However, since I am not sure how many interest rates I will be dealing with to begin with, I have tried to call these inputs using a loop as follows. > > In my R working directory, following files are there which are to be read as input.. > > > rate1_range.csv > rate2_range.csv > rate3_range.csv > rate4_range.csv > > rate1_probs.csv > rate2_probs.csv > rate3_probs.csv > rate4_probs.csv > > My R Code > > # _______________________________________________________________ > > n = no_of_Interest_Rates # This 'n' will be suppllied separately& can be anything. > > n= 4 # As mentioned, this input will be added seperately. > > for (i in 1:n) > > { > rate_[i] = read.csv('rate[i]_range.csv') > prob_[i] = read.csv('rate[i]_probs.csv') > } > > # End of code. > > However when I excute this code (here, n = 4), I get following error. > > Error in file(file, "r") : cannot open the connection > In addition: Warning message: > In file(file, "r") : > cannot open file 'rate[i]_range.csv': No such file or directory > > Obviously as usual I have written some stupid code. > > Please guide > > 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.
The easiest way to do this is to use an lapply: #in working directory filelist <- list.files() ranges <- lapply(filelist[grep(".*range\\.csv", filelist)], read.csv) probs <- lapply(filelist[grep(".*probs\\.csv", filelist)], read.csv) Hope this helps, Greg On 12/20/09 10:24 PM, Maithili Shiva wrote:> Dear R helpers, > > Suppose I am dealing with no of interest rates at a time and the no of interest rates I am selecting for my analysis is random i.e. it can be 2, can be 10 or even higher. The R-code I had written (with the guidance of R helpers obviously and I am really grateful to all of you) as of now is sort of hard coding when it comes to reading interest rates as an input e.g. > > ## 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') > > However, since I am not sure how many interest rates I will be dealing with to begin with, I have tried to call these inputs using a loop as follows. > > In my R working directory, following files are there which are to be read as input.. > > > rate1_range.csv > rate2_range.csv > rate3_range.csv > rate4_range.csv > > rate1_probs.csv > rate2_probs.csv > rate3_probs.csv > rate4_probs.csv > > My R Code > > # _______________________________________________________________ > > n = no_of_Interest_Rates # This 'n' will be suppllied separately& can be anything. > > n= 4 # As mentioned, this input will be added seperately. > > for (i in 1:n) > > { > rate_[i] = read.csv('rate[i]_range.csv') > prob_[i] = read.csv('rate[i]_probs.csv') > } > > # End of code. > > However when I excute this code (here, n = 4), I get following error. > > Error in file(file, "r") : cannot open the connection > In addition: Warning message: > In file(file, "r") : > cannot open file 'rate[i]_range.csv': No such file or directory > > Obviously as usual I have written some stupid code. > > Please guide > > 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. >-- Greg Hirson ghirson@ucdavis.edu Graduate Student Agricultural and Environmental Chemistry 1106 Robert Mondavi Institute North One Shields Avenue Davis, CA 95616 [[alternative HTML version deleted]]