[R] how do I name data frames and files according to the index of a for loop?

jim holtman jholtman at gmail.com
Thu Oct 8 13:42:38 CEST 2009

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?

More information about the R-help mailing list