On Mon, 27 Aug 2007, Ptit_Bleu wrote:


Hi,

I recently discovered the R program and I thought it could be useful to me.
I have to analyse data saved as .Px file (x between 0 and 8 - .P0 files have
18 lines at the beginning that I have to skip). New files are generated
everyday.

    relrfichiers<-dir(chemin, pattern=".P")

does not do that, though.  Better to use

    dir(chemin, pattern="\\.[0-8]$", full.names=TRUE)

or

    Sys.glob(file.path(chemin, "*.P[0-8]"))


This is my strategy :

In order to analyse the data, I first want to copy the new data in a
database in MySQL (which already contains the previous data).
So the first task is to compare the list of the files in the directory
(object : rfichiers) to the list of the files already saved (object :
tfichiers). The list containing the new files is then given by
nfichiers<-setdiff(rfichiers, tfichiers).

It sounds easy ...
... but it doesn't work !!!

Up to now, I'm am able to connect to MySQL and, if the file "tfichiers.r"
doesn't exist, I can copy data files to the MySQL database.
But if "tfichiers.r" already exists and there is no new file to save, it
ignores the condition if (nfichiers!="0") and save all the files of the
directory to the database.

What did you intend there? It is not a test of no difference, but a test that each element of the difference is not "0", and furthermore if() expects a test of length one, not the length of nfichiers. I suspect you intended to test length(nfichiers) > 0.

It often helps to print (or use str on) the objects you create. Try this on

    nfichiers
    nfichiers!="0"

Is it a problem with the way I save tfichiers or is it a problem with the
condition if (nfichiers!="0") ?

Saving in R save format with extension .r is going to confuse others. Extension .rda is conventional for save format (and I doubt you need an ascii save).

Could you please give me some advices to correct my script (written with
Tinn-R) ?

I thank you in advance for your help.
Have a nice week,
Ptit Bleu.

PS : Ptit Bleu means something like "Full Newbye" in french. So thanks to be
patient :-)
PPS : I hope you understand my french english

------------------------------------------


# Connexion a la base de donnees database de MySQL

library(DBI)
library(RMySQL)
drv<-dbDriver("MySQL")
con<-dbConnect(drv, username="user", password="password", dbname="database",
host="localhost")


# Creation des objets contenant la liste des fichiers (rel pour chemin
relatif)
# - dans le repertoire : objet rfichiers
# - deja traites : objet tfichiers
# - nouveaux depuis la derniere connexion : objet nfichiers
# chemin est le repertoire de stockage des donnees
# RWork est le repertoire de travail de R
# sep='' pour eviter l'ajout d'un espace apres Mydata/

setwd("D:/RWork")
chemin<-"d:/Mydata/"
relrfichiers<-dir(chemin, pattern=".P")
rfichiers<-paste(chemin,relrfichiers, sep='')
if (file.exists("tfichiers.r"))
 {
   tfichiers<-load("tfichiers.r")
   nfichiers<-setdiff(rfichiers,tfichiers)
 } else {
   nfichiers<-rfichiers
 }


# p0fichiers : fichiers avec l'extension .P0 (fichiers contenant des lignes
d'infos à ne pas charger)
# pxfichiers : fichiers avec les extensions P1, ..., P8 (sans infos au
debut)

if (nfichiers!="0")
{
 p0fichiers<-nfichiers[grep(".P0", nfichiers)]
 pxfichiers<-setdiff(nfichiers, p0fichiers)


# Fusion des colonnes jour et heure pour permettre de tracer des variations
en fonction du temps
# Chaque fichier contenu dans l'objet p0fichiers est chargé, en supprimant
les 18 premieres lignes,
# et on met dans l'objet jourheure la fusion de la colonne jour (V1) et de
la colonne heure (V2)
# L'objet jourheure est recopie dans la premiere colonne de donnees
# On supprime ensuite la deuxieme colonne (contenant les heures) qui est
maintenant superflue
# L'objet donnees est copié dans la base de donnees MySQL Mydata
# Remarque : R comprend le format jour/mois/annee - MySQL : annee/mois/jour
-> stockage en CHAR dans MySQL

 for (i in 1:length(p0fichiers))
   {
     donnees<-read.table(p0fichiers[i], quote="\"", sep=";", dec=",",
skip=18)
     jourheure<-paste(donnees$V1, donnees$V2, sep=" ")
     donnees[1]<-jourheure
     donnees<-donnees[,-2]
#  assignTable(con, "Datatable", donnees, append=TRUE) - Ne marche pas
     dbWriteTable(con, "Datatable", donnees, append=TRUE)
     rm(donnees, jourheure)
   }


# Idem avec les fichiers d'extension .Px en chargant toutes les lignes
(skip=0)
# Amelioration possible : creer une fonction avec en argument p0fichiers ou
pxfichiers

 for (i in 1:length(pxfichiers))
   {
     donnees<-read.table(pxfichiers[i], quote="\"", sep=";", dec=",",
skip=0)
     jourheure<-paste(donnees$V1, donnees$V2, sep=" ")
     donnees[1]<-jourheure
     donnees<-donnees[,-2]
#   assignTable(con, "Datatable", donnees, append=TRUE) - Ne marche pas
     dbWriteTable(con, Datatable", donnees, append=TRUE)
     rm(donnees, jourheure)
   }
}

tfichiers<-rfichiers
save(rfichiers, file="tfichiers.r", ascii=TRUE)
rm(list=ls())

# Deconnexion à MySQL

dbDisconnect(con)


--
Brian D. Ripley,                  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595
______________________________________________
R-help@stat.math.ethz.ch 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.

Reply via email to