[R] list to data frame by rows ...
Johann Petrak
johann at ai.univie.ac.at
Fri May 18 14:31:45 CEST 2001
I am not yet shutting up on this :)
Thanks for the suggestions ... problem is that the code was
given as an illustration, in reality do4split would do
some complicated magic calculations that yield several
interesting results of mixed type.
Peter Dalgaard BSA wrote:
>
> [...]
> > cl <- 1:2
> > names(cl) <- c("a","b")
> > as.data.frame(lapply(cl,function(i)sapply(result,"[[",i)))
> a b
> x x 2
> y y 4
>
The problem here is that the functionality gets pulled out of
do4split which isnt really possible in the *real* situation.
Patrick Connolly wrote:
>
> [...]
> If you'd like another approach, I kind of think I'd be inclined to do
> what you do in your do4split function in separate pieces, each one
> acting on one or more of the columns. I assume you want more rows
> than columns. The number of functions would be at most the number of
> parts you do in your do4split function, and then it's already in the
> form you require, so it's probably no longer to code, and maybe
> quicker to run.
>
As mentioned above, its not possible to split up do4split into
several pieces without doing the same calculations over and over
again.
So I guess there is no way around doing it iteratively. I would have
thought that adding the rows iteratively to an empty data frame
would be the obvious approach, but I am unable to make an empty
data frame:
data.frame(list(a=c(),b=c()))
Error in names<-.default(*tmp*, value = vnames) :
names attribute must be the same length as the vector
However, its possible to make an list with "0 rows":
ltmp<-list(a=c(),b=c())
for (s in splitted) {
print(s)
tmp <- do4split(s)
for (i in 1:length(tmp)) {
ltmp[[i]]<-c(ltmp[[i]],tmp[[i]])
}
}
I would be curious how efficient this is and what actually happens.
Will ltmp[[i]]<-c(ltmp[[i]],tmp[[i]]) just extend the column or
will the whole content be copied every time?
The final step
as.data.frame(ltmp)
again has the problem that the character columns will be converted
to levels. But when a column has class AsIs it wont:
data.frame(lapply(ltmp,I))
Johann
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list