[R] read

John McKown john.archie.mckown at gmail.com
Tue Nov 29 02:09:33 CET 2016


On Mon, Nov 28, 2016 at 4:23 PM, Ashta <sewashm at gmail.com> wrote:

> Hi all,
>
> I have a script that  reads a file (dat.csv)  from several folders.
> However, in some folders the file name is (dat) with out csv  and in
> other folders it is dat.csv.  The format of data is the same(only the
> file name differs  with and without "csv".
>
> Is it possible to read these files  depending on their name in one?
> like read.csv("dat.csv"). How can I read both type of file names?
>
> Thank you in advance
>
>
​I'd do something like this:

> files=c('dat.csv','dat')
> file2read=files[file.exists(files)][1]
> file2read
[1] "dat.csv"

You put the possible file names into the variable in the order of
preference. E.g. I prefer "dat.csv" over "dat" if by chance both exist.

> files=c('not.csv','not')
> file2read=files[file.exists(files)][1]
> file2read
[1] NA

​The above shows the result should none of the files exist. So if
"file2read" has an NA, then you go on to the next directory.​


-- 
Heisenberg may have been here.

Unicode: http://xkcd.com/1726/

Maranatha! <><
John McKown​

	[[alternative HTML version deleted]]



More information about the R-help mailing list