
Hi R community, 

First of all, I want to thank everybody to share their time solving R questions, You are great. Ok, for my questions, I've been looking for a solutions by myself, in forums but I'm just a little bit desesperate so I hope somebody can help me. I have built a code to read files from a directory. These files are named by a year (2004.csv, 2005.csv,...). When the code reads first file (2004.csv), inside this file, there is information about precipitation of every months and I calculate different variables like R square adjusted, p value or formula fit to the data from linear regression. The code do more things but for my question, to explain what I need, that part of the code is enough. I want to export to the text file, year and month apart of some other variables, something like this:

Year: 2004   Month: January  R1: 0.98   Pvalue: 0.03 ...
Year: 2004   Month: February  R1:0.78   Pvalue:0.12 ...

I've seen that I can use order sink() and cat(), so I would put those orders in my code, like this:

nfiles<- length(dir("directory where my files are"))   #Count file number

    for(year in 1:nfiles)                    #Read first file 
    {
        filename<-dir()[[year]]                #take first file and read filename, so if year is 1, then filename will be 2004, is year is 2, filename will be 2005,...
       
        clima<-read.csv2(filename, nrows=7) #open 2004.csv


So, if want to export year to my text file I would do


  for(year in 1:nfiles)                    #Read first file 

    {
        sink("directore where my text file is")

        filename<-dir()[[year]]                #take first file and
read filename, so if year is 1, then filename will be 2004, is year is
2, filename will be 2005,...

        cat(" Year: ",filename)
        sink()

        clima<-read.csv2(filename, nrows=7) #open 2004.csv

And in my text file would read

Year: 2004

Now, I want to the same to months. (I have built a for loop to read months inside for loop to read years). When I import a csv file I get something like this

     Januray    February ....
1        3.0         4.1    
2        1.4          3.7
3         0.2         1.5
4         6.7          4.1
.
.
.

I can use commands like clima$Januray or clima[[1]] but I just get precipitation values. However, I am not able to get the header of the column. If I would able to do that I could do the same as for years and export those headers to my text file. Does anyone know how I could do that? or does anyone know another way to do what I need? Would anyone use sink() and cat() commands to create a summary text like the one I need to do?. Probably my for loop is not the best, I am still a beginner with R, and probably there are some better forms to express in R what I need but I am working alone so there is nobody in person to help me so I apologize for my simple questions. Thanks in advance.

Lucas



 		 	   		  
_________________________________________________________________


	[[alternative HTML version deleted]]


