Ptit_Bleu
2007-Aug-27 08:22 UTC
[R] Problem with save or/and if (I think but maybe not ...)
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.
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.
Is it a problem with the way I save tfichiers or is it a problem with the
condition if (nfichiers!="0") ?
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)
--
View this message in context:
http://www.nabble.com/Problem-with-save-or-and-if-%28I-think-but-maybe-not-...%29-tf4333945.html#a12343236
Sent from the R help mailing list archive at Nabble.com.
Hi,
I realized that my first message and the script were (maybe) too long and
difficult to read.
So I tested this shorter one :
-------------------------------------
setwd("D:/RWork")
chemin<-"d:/Mydata/"
relrfichiers<-dir(chemin, pattern=".P")
rfichiers<-paste(chemin,relrfichiers, sep='')
tfichiers<-rfichiers
save("tfichiers", file="tfichiers.r", ascii=TRUE)
if (file.exists("tfichiers.r"))
{
tfichiers<-load("tfichiers.r")
nfichiers<-setdiff(rfichiers,tfichiers)
}
------------------------
The result is :
nfichiers is equal to rfichiers
and when I ask tfichiers, I obtain ... "tfichiers" :-(
I read the ?save and saw the warning about the arguments but I have no idea
how to solve this problem which must be a basic one (but do not forget that
I'm a newbye and that I'm french :-)
Thans again for your comments and help,
Ptit Bleu.
--
View this message in context:
http://www.nabble.com/Problem-with-save-or-and-if-%28I-think-but-maybe-not-...%29-tf4333945.html#a12343633
Sent from the R help mailing list archive at Nabble.com.
Prof Brian Ripley
2007-Aug-27 09:02 UTC
[R] Problem with save or/and if (I think but maybe not ...)
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, ripley at stats.ox.ac.uk 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
Ptit_Bleu
2007-Aug-27 09:52 UTC
[R] Problem with save or/and if (I think but maybe not ...)
Dera Prof Ripley, You wrote : 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. And of course, you were right. With the condition length(nfichiers) > 0, there is no more warning. And I tested manually length(nfichiers)>0 for different cases and it gave the result I expected. But still I have a problem with the save and the retrieve of tfichiers. I keep on looking at help file and testing manually alternative scripts ... Hoping to read you again, Ptit Bleu. -- View this message in context: http://www.nabble.com/Problem-with-save-or-and-if-%28I-think-but-maybe-not-...%29-tf4333945.html#a12344227 Sent from the R help mailing list archive at Nabble.com.
Ptit_Bleu
2007-Aug-27 11:15 UTC
[R] [SOLVED] save/load - I finally found (to be honnest : jholtman found)
The post of jholtman gave me the solution :
http://www.nabble.com/problems-saving-and-loading-%28PLMset%29-objects-tf4179541.html#a11885136
Like Quin Wills, I was trying to assign tfichiers.rda to tfichiers.
I've just write load("tfichiers.rda") instead of
tfichiers<-("tfichiers.rda")
And now it works ... for this part (because if new files are only .P0, there
is a problem when the script try to read .P(not 0) file as there is none.
But this is not so difficult to solve even for me (I think, well, I hope).
Thanks to Prof Ripley and to all people helping people like me (maybe one
day I will also be able to help people).
Have a nice week,
Ptit Bleu.
--
View this message in context:
http://www.nabble.com/Problem-with-save-or-and-if-%28I-think-but-maybe-not-...%29-tf4333945.html#a12345123
Sent from the R help mailing list archive at Nabble.com.
Reasonably Related Threads
- how to write nicely a condition on a loop "for" (that is, not like I did)
- Must be obvious but not to me : problem with regular expression
- Strage result with an append/strptime combination
- multcomp, simint, simtest and computation duration
- linear regression: Is there a way to get the results of lm as variables