[R] how to write nicely a condition on a loop "for" (that is, not like I did)
Ptit_Bleu
ptit_bleu at yahoo.fr
Mon Aug 27 15:33:28 CEST 2007
Hi again,
This is the follow of my post "Problem with save or/and if (I think but
maybe not ...)".
In this post, I wrote that I solved my main problem. And it is true.
I also wrote that there was still another problem, which I managed to solve.
But I think there must be another way to solve it taking advantages of the R
language (which I don't master at all), that is with less "if" tests.
To sum up :
nfichiers is a list of files (with .P0 or .Px (x>0) extension) I have to
copy to a database.
nfichiers can also be "0" if there is no file to copy
p0fichiers is the list of files having the .P0 extension if there are such
files to copy
And p0fichiers can also be "0" if there are only .Px files to copy
So, before doing the "for" loop, I want to test if p0fichiers really
contains something.
Thanks for your comments and your advices to improve this script.
Ptit Bleu.
-------------------------------------------------------------------------
So here is my solution :
p0fichiers<-"0" #initialization of
p0fichiers
if (length(nfichiers)>0) # if nfichiers contains
file names
{
if (length(grep(".P0", nfichiers))>0) {p0fichiers<-nfichiers[grep(".P0",
nfichiers)]} #look if there is .P0
if (p0fichiers[1]>"0") # if .P0 has been updated
with the test above
{
for (i in 1:length(p0fichiers)) # do the loop "for"
{
donnees<-read.table(p0fichiers[i], quote="\"", sep=";",
dec=",", skip=18)
jourheure<-paste(donnees$V1, donnees$V2, sep=" ")
donnees[1]<-jourheure
donnees<-donnees[,-2]
rm(donnees, jourheure)
}
}
}
--
View this message in context: http://www.nabble.com/how-to-write-nicely-a-condition-on-a-loop-%22for%22-%28that-is%2C-not-like-I-did%29-tf4335310.html#a12347016
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list