a question for save CSV files in the 'for loop' codes
The problem is you're using the same filename for each call to write. The for loop is unaware of the i in "F:/pixel_no[i].csv" as it is hidden by the quote marks. You need to use the paste function to create a new file name for each iteration. Will Morris Masters of Philosophy candidate Vesk Plant Ecology Lab The School of Botany The University of Melbourne Australia Phone: +61 3 8344 0120 http://www.botany.unimelb.edu.au/vesk/
On 25/12/2009, at 19:38, Zongshan Li <zongshan_li at yahoo.com> wrote:
Dear the friends of R-Ecology:
I am a beginner for R language, I have a question, maybe very
simple, to save CSV file in the ?for loop? function.
The code is just following:
rownames(pre1901_1920)<-pre1901_1920[,1]
pixel_no<-rownames(pre1901_1920)
for(i in 1:100)
{
data01<-matrix(pre1901_1920[i,2:241],ncol=12,byrow=T)
data02<-matrix(pre1921_1940[i,2:241],ncol=12,byrow=T)
data03<-matrix(pre1941_1960[i,2:241],ncol=12,byrow=T)
data04<-matrix(pre1961_1980[i,2:241],ncol=12,byrow=T)
data05<-matrix(pre1981_2000[i,2:241],ncol=12,byrow=T)
data<-rbind(data01,data02,data03,data04,data05)
colnames(data)<-c
('j
an','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec')
rownames(data)<-1901:2000
write.csv(data,file="F:/pixel_no[i].csv")
}
The variables of ?pre1901_1920?, ?pre1921_1940?,
?pre1941_1960?, ?pre1961_1980? and ?pre1981_2000? are
downloaded precipitation data from CRU TS 2.1 Global Climate dataset
for a special 5*5 degree region lasting from 1901-2000.
The first column of each variable is the special values for 100
pixels of this CRU dataset (such as 51447, 51582, 51584, 51573
etc.), while the rows (2:241) of each variable are the monthly
precipitation data for 20 years.
I want to reshape the original dataset to the dataset for each
pixel, which is a 100-raw and 12-colunm matrix to represent the
monthly precipitation data lasting from 1901 to 2000 in each pixel;
and each such matrix (100*12) is subsequently saved by a special CSV
file(the file name is the value of that pixel).
All the codes run well, except for the last row (I am wondering why
only save a single CSV file?). My question is that how to save the
100 matrixes (100*12) generated in the ?for loop? function to
different CSV files with the pixel values as the file title names.
Any help is strongly appreciated,
thank you in advance,
Merry Christmas to you!
----------------------
Zongshan, Li
2009-12-25
Research Center for Eco-Environmental Sciences,Chinese Academy of
Sciences
Shuangqing Road, Haidian street, Beijing, 100085
Tel: 86-10-13699145748
QQ: 1141958023
email: ZongShan_Li at yahoo.com
website: http://www.rcees.ac.cn/
[[alternative HTML version deleted]]
_______________________________________________ R-sig-ecology mailing list R-sig-ecology at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology