read

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

read

Ashta
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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 | Threaded
Open this post in threaded view
|

Re: read

John McKown
On Mon, Nov 28, 2016 at 4:23 PM, Ashta <[hidden email]> 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]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 | Threaded
Open this post in threaded view
|

Re: read

Jeff Newmiller
In reply to this post by Ashta
No, and yes, depending what you mean.

No, because you have to supply the file name to open it... you cannot directly use wildcards to open files.

Yes,  because the list.files function can be used to match all file names fitting a regex pattern, and you can use those filenames to open the files.

E.g.

fns  <- list.files( pattern="dat(\\.[^.]+)$" )
dtaL <- lapply( fns, function(fn){ read.csv( fn, stringsAsFactors=FALSE ) } )

If you only expect one file to be in any given directory, you can skip the lapply and just read the file, or you can extract the data frame from the list using dtaL[[ 1 ]].

?list.files
?regex for help on patterns
--
Sent from my phone. Please excuse my brevity.

On November 28, 2016 2:23:23 PM PST, Ashta <[hidden email]> 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
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 | Threaded
Open this post in threaded view
|

Re: read

Val-17
Hi Jeff  and John,

Thank you for your response.
In each folder, I am expecting a single file name (either dat or
dat.csv).v  so will this work?


Is the following correct?
fns  <- list.files(mydir)
    if (is.element(pattern="dat(\\.[^.]+)$",fns ))

Thank you again.

On Mon, Nov 28, 2016 at 7:20 PM, Jeff Newmiller
<[hidden email]> wrote:

> No, and yes, depending what you mean.
>
> No, because you have to supply the file name to open it... you cannot directly use wildcards to open files.
>
> Yes,  because the list.files function can be used to match all file names fitting a regex pattern, and you can use those filenames to open the files.
>
> E.g.
>
> fns  <- list.files( pattern="dat(\\.[^.]+)$" )
> dtaL <- lapply( fns, function(fn){ read.csv( fn, stringsAsFactors=FALSE ) } )
>
> If you only expect one file to be in any given directory, you can skip the lapply and just read the file, or you can extract the data frame from the list using dtaL[[ 1 ]].
>
> ?list.files
> ?regex for help on patterns
> --
> Sent from my phone. Please excuse my brevity.
>
> On November 28, 2016 2:23:23 PM PST, Ashta <[hidden email]> 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
>>
>>______________________________________________
>>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>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.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 | Threaded
Open this post in threaded view
|

Re: read

Jeff Newmiller
Doesn't look right to me... you are likely to need to change something to handle the multiple directories thing somehow, but I don't know why you made the changes you did make to my suggestion.
--
Sent from my phone. Please excuse my brevity.

On November 28, 2016 5:56:04 PM PST, Val <[hidden email]> wrote:

>Hi Jeff  and John,
>
>Thank you for your response.
>In each folder, I am expecting a single file name (either dat or
>dat.csv).v  so will this work?
>
>
>Is the following correct?
>fns  <- list.files(mydir)
>    if (is.element(pattern="dat(\\.[^.]+)$",fns ))
>
>Thank you again.
>
>On Mon, Nov 28, 2016 at 7:20 PM, Jeff Newmiller
><[hidden email]> wrote:
>> No, and yes, depending what you mean.
>>
>> No, because you have to supply the file name to open it... you cannot
>directly use wildcards to open files.
>>
>> Yes,  because the list.files function can be used to match all file
>names fitting a regex pattern, and you can use those filenames to open
>the files.
>>
>> E.g.
>>
>> fns  <- list.files( pattern="dat(\\.[^.]+)$" )
>> dtaL <- lapply( fns, function(fn){ read.csv( fn,
>stringsAsFactors=FALSE ) } )
>>
>> If you only expect one file to be in any given directory, you can
>skip the lapply and just read the file, or you can extract the data
>frame from the list using dtaL[[ 1 ]].
>>
>> ?list.files
>> ?regex for help on patterns
>> --
>> Sent from my phone. Please excuse my brevity.
>>
>> On November 28, 2016 2:23:23 PM PST, Ashta <[hidden email]> 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
>>>
>>>______________________________________________
>>>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>>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.
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.