Dear Ivan and Greg, thaks a lot!

sorry for late reply, both ways work fine! I have to say that maybe Greg's
is a little faster (but I am working with a relatively small amount of
data, approx 130 xls files) so I do not notice remarkable differences.

The only suggestion I have is to add to the read.xlsx(...) part either:

stringsAsFactors=FALSE

or:

colClasses= c("numeric","character", ...)

to make the rbind operation robust (otherwise NA could be created)
thanks!
Mario


On Tue, Mar 12, 2013 at 10:17 PM, Greg Snow <538...@gmail.com> wrote:

> The only real improveent I can see over Ivan's solution is to use lapply
> instead of the loop (this may just be person preference though).
>
> Something like:
>
> list_df <- lapply( lista_rea_c, function(x) read.xls( file=
> paste0(path,x,"/",x,".xls"),1,header=TRUE,as.data.frame=TRUE))
> my_df <- do.call(rbind, list_df)
>
> You could even do that as a single line if you really wanted to, but it
> would be less readable.  You could also make it a little more readable by
> putting the paste on its own line to create all the path/filenames in a
> variable to pass to lapply.
>
>
>
> On Tue, Mar 12, 2013 at 9:06 AM, Ivan Calandra <
> ivan.calan...@u-bourgogne.fr
> > wrote:
>
> > Hi Mario!
> >
> > I'm not really familiar with this kind of manipulations, but I think you
> > can do it more or less like this (some people on this list might give a
> > more detailed answer):
> >
> > #Create an empty named list
> > list_df <- vector(mode="list", length=length(lista_rec_c))
> > names(list_df) <- lista_rea_c    ##or some part of it using gsub or
> > something similar
> >
> > #Import
> > for (i in lista_rea_c) {
> >     list_df[[i]] <- read.xlsx(...)
> > }
> >
> > #rbind
> > do.call(rbind, list_df)
> >
> > This probably won't work like this exactly but you should be able to make
> > the modifications.
> >
> > 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.calan...@u-bourgogne.fr
> > http://biogeosciences.u-**bourgogne.fr/calandra<
> http://biogeosciences.u-bourgogne.fr/calandra>
> >
> > Le 12/03/13 15:52, A M Lavezzi a écrit :
> >
> >> Hello everybody
> >>
> >> I have the following problem. I have to load a number of xls files from
> >> different folders (each xls file has the same number of columns, and
> >> different numbers of rows). Each xls file is named with a number, i.e.
> >> 12345.xls and is contained in a folder with same name, say 12345)
> >>
> >> Once loaded, I want to rbind all of them to obtain a single database.
> >>
> >> I think I successfully did the first part, using "assign":
> >>
> >> for (i in lista_rea_c){
> >>
> >> name=paste("reaNum",i,sep="",**collapse=NULL)
> >>
> >> assign(name,read.xlsx(file=**paste(path,i,"/",i,".xls",sep=**
> >> "",collapse=NULL),1,header=**TRUE,as.data.frame=TRUE))
> >>
> >> }
> >>
> >> where lista_rea_c contains the "numbers" and is obtained
> >> as: lista_rea_c = list.files(path = "/Users/mario/Dropbox/..., and path
> is
> >> defined elsewhere
> >> At this point I have a number of variables, names as "reaNum12345" ecc.
> >>
> >> I would like to rbind all of them, but what I get is that I rbind
> >> "reaNum12345", i.e. the variable name and not the data it contains.
> >>
> >> Can anyone help?
> >>
> >> thanks!
> >> Mario
> >>
> >>
> >>
> >>
> >> ______________________________**________________
> >> R-help@r-project.org mailing list
> >> https://stat.ethz.ch/mailman/**listinfo/r-help<
> https://stat.ethz.ch/mailman/listinfo/r-help>
> >> PLEASE do read the posting guide http://www.R-project.org/**
> >> posting-guide.html <http://www.R-project.org/posting-guide.html>
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
> > ______________________________**________________
> > R-help@r-project.org mailing list
> > https://stat.ethz.ch/mailman/**listinfo/r-help<
> https://stat.ethz.ch/mailman/listinfo/r-help>
> > PLEASE do read the posting guide http://www.R-project.org/**
> > posting-guide.html <http://www.R-project.org/posting-guide.html>
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>
>
> --
> Gregory (Greg) L. Snow Ph.D.
> 538...@gmail.com
>
>         [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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.
>
>


-- 
-- 
PLEASE NOTICE NEW EMAIL ADDRESS AND HOME PAGE URL

Andrea Mario Lavezzi
Dipartimento di Studi su Politica, Diritto e Società
Università di Palermo
Piazza Bologni 8
90134 Palermo, Italy
tel. ++39 091 23892208
fax ++39 091 6111268
skype: lavezzimario
email: mario.lavezzi (at) unipa.it
web: http://www.unipa.it/~mario.lavezzi

        [[alternative HTML version deleted]]

______________________________________________
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.

Reply via email to