Ken Ervin
2009-Oct-08 03:10 UTC
[R] how do I name data frames and files according to the index of a for loop?
Thanks in advance for reading my question. This is my first time working with R, though I have some intro-level experience with other languages. I am writing a program that performs a certain set of calculations on each row of a list of data, and here's what I have so far: for (i in 1:2858) { Calc_1 <- some_stuff Calc_2 <- some_more_stuff Calc_3 <- some_other_stuff Data_frame_name <- data.frame(Calc_1, Calc_2, Calc_3) write.table(Data_frame_name,file="~/file/goes/here.csv", append = FALSE, sep = "\t",row.names = FALSE, col.names = c("Calc_1", "Calc_2", "Calc_3"),qmethod = "double") } The naming of the data frame and the writing of the file go swimmingly, if I do it ONCE, outside of the for loop. I want to embed the process in the for loop written above, run it 2858 times, and get that many data frames and .csv files that I can then manipulate. In order to do so, I would like to name the data frames and/or files by their respective index, to get: Data_frame_name1 Data_frame_name2 Data_frame_name3 ... Data_frame_name2858 I would also like to produce the .csv files: ~/file/goes/here1.csv ~/file/goes/here2.csv ~/file/goes/here3.csv ... ~/file/goes/here2858.csv I've been noodling over this for a while and have looked all over but I can't seem to get the syntax right. Any help at all would be appreciated. Thanks again! -Ken Ervin
Dieter Menne
2009-Oct-08 06:24 UTC
[R] how do I name data frames and files according to the index of a for loop?
Ken Ervin wrote:> > > for (i in 1:2858) { > ... > write.table(Data_frame_name,file="~/file/goes/here.csv", append > = FALSE, sep = "\t",row.names = FALSE, col.names = c("Calc_1", "Calc_2", > "Calc_3"),qmethod = "double") > > } > > >use something like filename = paste("~/file/myfile",i,".csv",sep="") then ... file = filename. Dieter -- View this message in context: http://www.nabble.com/how-do-I-name-data-frames-and-files-according-to-the-index-of-a-for-loop--tp25798724p25799032.html Sent from the R help mailing list archive at Nabble.com.
jim holtman
2009-Oct-08 11:42 UTC
[R] how do I name data frames and files according to the index of a for loop?
Do you really need to keep all 2858 dataframes around, or are you just using them to create the object to write out? If you really have to keep that many, I would suggest that you use a list since you will really polute your workspace with that many objects from the standpoint if you ever wanted to do 'ls()' to see what objects you had in the workspace. On Wed, Oct 7, 2009 at 11:10 PM, Ken Ervin <ervinjudo at gmail.com> wrote:> Thanks in advance for reading my question. > This is my first time working with R, though I have some intro-level > experience with other languages. > I am writing a program that performs a certain set of calculations on each > row of a list of data, and here's what I have so far: > > for (i in 1:2858) { > ? ? ? ? ? ? Calc_1 <- some_stuff ? ? ? ? Calc_2 <- some_more_stuff > ? ? ? Calc_3 <- some_other_stuff > > ? ? ? Data_frame_name <- data.frame(Calc_1, Calc_2, Calc_3) > ? ? ? ? write.table(Data_frame_name,file="~/file/goes/here.csv", append > FALSE, sep = "\t",row.names = FALSE, col.names = c("Calc_1", "Calc_2", > "Calc_3"),qmethod = "double") > ?} > > > The naming of the data frame and the writing of the file go swimmingly, if I > do it ONCE, outside of the for loop. ?I want to embed the process in the for > loop written above, run it 2858 times, and get that many data frames and > .csv files that I can then manipulate. ?In order to do so, I would like to > name the data frames and/or files by their respective index, to get: > Data_frame_name1 > Data_frame_name2 > Data_frame_name3 > ... > Data_frame_name2858 > > I would also like to produce the .csv files: > ~/file/goes/here1.csv > ~/file/goes/here2.csv > ~/file/goes/here3.csv > ... > ~/file/goes/here2858.csv > > I've been noodling over this for a while and have looked all over but I > can't seem to get the syntax right. ?Any help at all would be appreciated. > Thanks again! > -Ken Ervin > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?