[R] importing data

Ivan Calandra
Sun, 27 Jan 2013 14:57:56 -0000

Hi Ray!

I'm insisting with list.files...!

What about like this (untested)?
file_names <- list.files(path="C:/.../data",
pattern=".dat$",
full.names=TRUE)
list_of_dataset <- do.call(read.table, file_names)

Let me know if this helps!
Ivan

--
Ivan CALANDRA
Université de Bourgogne
UMR CNRS/uB 6282 Biogéosciences
6 Boulevard Gabriel
21000 Dijon, FRANCE
+33(0)3.80.39.63.06
ivan.calandra@u-bourgogne.fr
http://biogeosciences.u-bourgogne.fr/calandra

Le 26/01/13 10:03, Ray Cheung a écrit :
> Thanks for your commands, Ivan and Michael! However, I am still not
> producing the right codes. Would you please help me on this? I've
> written the following codes. Please comment. Thank you very much.
> Task: Reading data1.dat to data1000.dat (with missing files) into R.
> Missing files can be omitted in the list.
> ###FUNCTION TO READ FILES
> little_helpful <- function(n) {
> file_name <- paste0("C:/.../data", n, ".dat")
> read.table(file_name)
> }
> ###RETURN AN OBJECT WHICH CHECKS FOR THE EXISTENCE OF FILES
> check  <- function(n) {
> a <- ifelse(file.exists(paste0("C:/.../data", n,
".dat")), 1, 0)
> a
> }
> ###Combining the functions
> IMPORT <- function(n) {
>    L <- check(1:n)
>    for (i in 1:n) {
>       if (L[i] == 1)
>       list_of_datasets <- lapply(i, little_helpful) else
> list_of_datasets <- 0
>       }
>    list_of_datasets
>    }
> Thanks for all comments.
> Best Regards,
> Ray
>
> On Fri, Jan 25, 2013 at 5:48 PM, Ivan Calandra
> <ivan.calandra@u-bourgogne.fr
<mailto:ivan.calandra@u-bourgogne.fr>>
> wrote:
>
>     Hi,
>
>     Not sure this is what you need, but what about list.files()?
>     It can get you all the files from a given folder, and you could
>     then work this list with regular expressions for example.
>
>     HTH,
>     Ivan
>
>     --
>     Ivan CALANDRA
>     Université de Bourgogne
>     UMR CNRS/uB 6282 Biogéosciences
>     6 Boulevard Gabriel
>     21000 Dijon, FRANCE
>     +33(0)3.80.39.63.06 <tel:%2B33%280%293.80.39.63.06>
>     ivan.calandra@u-bourgogne.fr
<mailto:ivan.calandra@u-bourgogne.fr>
>     http://biogeosciences.u-bourgogne.fr/calandra
>
>     Le 25/01/13 10:00, R. Michael Weylandt a écrit :
>
>         On Fri, Jan 25, 2013 at 6:11 AM, Ray Cheung <ray1728@gmail.com
>         <mailto:ray1728@gmail.com>> wrote:
>
>             Dear Michael,
>
>             Thanks for your codes. However, lapply does not work in my
>             case since I've
>             some files missing in the data (say, the file
>             data101.dat). Do you have any
>             suggestions on this?? Thank you very much.
>
>         You could simply add a test using file.exists() but I'm not
>         sure what
>         you want to do with the M matrix then -- omit the slice (so
>         the others
>         are all shifted down one) or fill it entirely with NA's.
>
>         Michael
>
>         ______________________________________________
>         R-help@r-project.org <mailto:R-help@r-project.org> mailing
list
>         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.
>
>
>     ______________________________________________
>     R-help@r-project.org <mailto:R-help@r-project.org> mailing list
>     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.
>
>

	[[alternative HTML version deleted]]