Skip to content

importing data

7 messages · R. Michael Weylandt, Ray Cheung, Ivan Calandra +1 more

#
On Wed, Jan 23, 2013 at 9:16 AM, Ray Cheung <ray1728 at gmail.com> wrote:
Hi Ray,

I'm pretty sure you don't mean "named" M[1,,] etc. but rather that
there's only one object M and that's how the slices come into
existence:

What you'll want to do is something like this:

little_helpful_function(n){
    file_name <- paste("data", n, ".dat", sep = "")
    read.dta(file_name, ##OTHER PARAMETERS)
}

list_of_datasets <- lapply(1:1000, little_helpful_function)

output <- do.call(c, list_of_datasets)

dim(output) <- c(dim(list_of_datasets[[1]]), 1000)

Or something like that. Note that I'm not quite sure what a dta file
is, so I'll leave it to you to find an appropriate read.dta function.

Feel free to write back (cc'ing the list) if you don't understand all
of the above.

Cheers,
Michael
1 day later
#
On Fri, Jan 25, 2013 at 6:11 AM, Ray Cheung <ray1728 at gmail.com> wrote:
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
#
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
ivan.calandra at u-bourgogne.fr
http://biogeosciences.u-bourgogne.fr/calandra

Le 25/01/13 10:00, R. Michael Weylandt a ?crit :
#
Hi Ray,
Comments below:
On 26 January 2013 09:03, Ray Cheung <ray1728 at gmail.com> wrote:
[snip]
Too complex. Why not just use file.exists directly?
Too complex here too. I suggest something like:

M <- list()
for (i in 1:n) {
if (file.exists(file_name)) M[i] <- read.table(file_name)
}

R gurus don't like for() loops, but they are easy for humans to understand.
If this doesn't work, post the error message.