[R] how to import several files every day

Jim Lemon jim at bitwrit.com.au
Mon Apr 22 10:34:57 CEST 2013


On 04/22/2013 10:14 AM, Martin Lavoie wrote:
> Hi Jim
>
> thanks for the help. I think I understand all the steps you suggested. I
> tried an example with your code but there is something not working I
> think. See below.
>
> all.filenames<-list.files(__path="pathtofile", full.names=TRUE) /#this
> step works/
> old.filenames<-read.table("__old.filenames.tab") /#this step works/
> filenames<-all.filenames[!(__all.filenames %in% old.filenames)]/## I
> think the problem is here because all.filenames = filenames (also
> checked with str()), so I import all the files (old and new files added
> to my folder) each time I run the code./
> write.table(all.filenames,__file="old.filenames.tab",row.__names=FALSE)
> /#this step works/
>
> Any suggestion?

Hi Martin,
This is what I get on my system:

 > all.filenames<-list.files(pattern="[.]csv")
 > all.filenames
  [1] "abc.csv"              "codseq.csv"           "eyeguess.csv"
  [4] "eyeresults.csv"       "eyes_data.csv"        "fmsdf.csv"
  [7] "obsnodecol5.csv"      "old.filenames.csv"    "Porzio.csv"
[10] "rg_test.csv"          "water40.csv"          "water_content_40.csv"
# 12 files with a .csv extension
# create a new file with a .csv extension
 > system("touch fntest.csv")
# I skipped the writing and reading, but this seems okay for you
 > old.filenames<-all.filenames
# now get the new listing with the new file
 > all.filenames<-list.files(pattern="[.]csv")
 > all.filenames 

  [1] "abc.csv"              "codseq.csv"           "eyeguess.csv"
  [4] "eyeresults.csv"       "eyes_data.csv"        "fmsdf.csv"
  [7] "fntest.csv"           "obsnodecol5.csv"      "old.filenames.csv"
[10] "Porzio.csv"           "rg_test.csv"          "water40.csv"
[13] "water_content_40.csv"
# now there are 13 files with a .csv extension
 > filenames<-all.filenames[!(all.filenames %in% old.filenames)]
# only the new file is now in filenames
 > filenames
[1] "fntest.csv"

Are you sure that you want those underscores in your code? I don't think 
that "__old.filenames" and "old.filenames" will give you the same file.

Jim



More information about the R-help mailing list