s-news
[Top] [All Lists]

read a file with name the current date automatically

To: s-news@wubios.wustl.edu
Subject: read a file with name the current date automatically
From: Spyros Vrontos <svrontos@aueb.gr>
Date: Sat, 24 Feb 2001 20:47:52 +0200
Hi all.

I would like to thank Mark Fisher, Jean  Adams, Phil Spector,
Patrick Connolly, Nick Ellis, Andrea Spano, for  giving very 
interesting  and helpful answers to my question.

Below there is the original question and the answers given.
Thanks a lot.
Spyros.


The original question was:

I would like every working  day to import a file using read.table.
The file will be in D:\\DATA\\2001\\
and it will be named as the paste of "dat", of the current date 
(yymmdd) and the ".txt" extension. The name of the object will
be the paste of "dat" and the current date.

for example:
current <- "dat010222.txt"
filename <- paste("D:\\DATA\\2001\\", current, sep="")
dat010222 <-  read.table(filename,header = TRUE, sep = c(1, 11, 21, 31 ,
41),skip=1)

Is there a way for this to be done automatically for each new day,
using a function like date()?

And the answers were:

From: Mark Fisher
Here is one way to generate the date you seem to want.

datelist<-unlist(unpaste(date(),sep=" "))
current<-paste("dat",format(dates(paste(datelist[2],"/",datelist[3],"/",date
list[5])),format="mdy"),".txt",sep="")

From: Jean Adams

A function like this should work for you ...

dat.name <- function() {
    ds <- date()
    mon <- substring(ds, 5, 7)
    m <- match(mon, month.abb)
    mm <- if(m<10) paste("0", m, sep="") else as.character(m)
    dd <- substring(ds, 9, 10)
    yy <- substring(ds, 23, 24)
    paste("dat", yy, mm, dd, sep="")
    }

Then you could use it like this ...
current <- paste(dat.name(), ".txt", sep="")
filename <- paste("D:\\DATA\\2001\\", current, sep="")
assign(dat.name, read.table(filename,header = TRUE, sep = c(1, 11, 21, 31
,41),skip=1)


From: Phil Spector 

datetostr <- function()
{
        thedate <- date()
        n <- nchar(thedate)
        str <- substring(thedate, 1:n, 1:n)
        str <- paste(sapply(str, function(x)
        ifelse(x == " ", "\n", x)), collapse = "")
        parts <- string.break.line(str)[[1]]
        parts <- parts[grep("[^ ][^ ]*", parts)]
        monnum <- c(paste("0", 1:9, sep = ""), "10", "11", "12")
        names(monnum) <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
                "Aug", "Sep", "Oct", "Nov", "Dec")
        day <- ifelse(nchar(parts[3]) == 1, paste("0", parts[3], sep = ""),
                parts[3])
        paste(substring(parts[6], 3, 4), monnum[parts[2]], day, sep = "")
}

From: Patrick Connolly

It's easy to do it on a unix system, but you could do a more
complicated version with the corresponding DOS calls.

In unix I would use 

current_unix("date +%y%m%d")
filename <- paste("D:\\DATA\\2001\\dat", current, "txt",sep="")

There might be a similar way in DOS to get the same format.  Lots of
things in DOS are similar.  Otherwise, you could do some text
manipulation of what DOS gives you with paste(), substring() and
unpaste().  You'll need to use parse to deal with the LHS of this
part:

dat010222 <-  read.table(filename,header = TRUE, sep = c(1, 11, 21, 31 ,
41),skip=1)

I don't know just what format DOS gives you so I can't make a good
suggestion.

And also  from Patrick Connolly

I tinkered a bit more with this in unix.  You should be able to modify
it to make it work in Windows.

This will work if your datafile is in your working directory.  It will
put the dataframe is the corresponding .Data directory:

function()
{
    current <- unix("date +%y%m%d")
    new.object.name <- paste("dat", current, sep = "")
    filename <- paste("dat", current, "txt", sep = ".")
    dat.today <- read.table(filename, T, as.is = T, sep = "\t", row.names = 
       NULL)
    dbwrite(search()[1], new.object.name, dat.today)
    detach(".Data")
    attach(".Data", pos = 1)
}

Seems to be necessary to reattach the .Data directory otherwise the
object is not visable.  I've modified the use of read.table() to suit
the file I was playing with, so you'll need to change it back.

If I knew what the options in DOS were for date, I might be able to
make that part work too.  Anyway, in S, you should be able to get the
bits together into the same form.

From: Nick Ellis

First let's get the date string:

> today()
[1] 23 Feb 2001
> dates(today())
[1] 02/23/2001
> as.character(dates(today()))
[1] "02/23/2001"
> unpaste(as.character(dates(today())),sep="/")
[[1]]:
[1] "02"

[[2]]:
[1] "23"

[[3]]:
[1] "2001"

> unlist(unpaste(as.character(dates(today())),sep="/"))
[1] "02"   "23"   "2001"
> paste(unlist(unpaste(as.character(dates(today())),sep="/")),collapse="")
[1] "02232001"
>

Having got the date string the rest is easy:

> d <-
paste(unlist(unpaste(as.character(dates(today())),sep="/")),collapse="")
> current <- paste("dat",d,".txt",sep="")
> filename <- paste("D:\\DATA\\2001\\", current, sep="")
> datname <- paste("dat", d, sep="")
> assign(datname, read.table(filename, header = T, sep = c(1, 11, 21, 31 ,
41), skip=1), where=1)


and from: Andrea Spano

something like this should work but you'll have to run

> loadit()

everytime you want to import a new file

Ciao

Andrea

loadit <- function(){
current <- paste("dat", as.character(dates(paste(substring(date(),
9,10),substring(date(), 5,7),substring(date(), 23,24), sep = "/"),format =
"d/m/y", out = "ymd")), ".txt", sep = "")
filename <- paste("D:\\DATA\\2001\\", current, sep="")
assign(substring(current, 1, 9), filename )
}






<Prev in Thread] Current Thread [Next in Thread>