[R] list to data frame by rows ...

Johann Petrak johann at ai.univie.ac.at
Thu May 17 15:13:08 CEST 2001


I still dont get the "feel" of R, maybe I am thinking to
complicated/LISP-damaged ...

I start with a dataframe like this:
df<-data.frame(a=c("x","x","y","y"),b=c(1,2,3,4))
df
  a b
1 x 1
2 x 2
3 y 3
4 y 4

now I split by column 1:
splitted_split(df,df[[1]])
splitted
$x
  a b
1 x 1
2 x 2

$y
  a b
3 y 3
4 y 4

This gives me a list of data frames. For each data frame I want to 
apply a funtion which returns several results

Here an arbitrary illustrative example:
do4split <- function(x) { list(as.character(x[[1]][1]), x[[2]][2]) }

It seems that using list here is the only way to return several
results of different type (with c() all my result values would
get converted to character)

result<-lapply(splitted,d4split)

Result now seems to be what I want: a list of lists where each sublist
contains the results passed back from do4split for each entry in
splitted.

result
$x
$x[[1]]
[1] "x"

$x[[2]]
[1] 2


$y
$y[[1]]
[1] "y"

$y[[2]]
[1] 4

Now I want to "transpose" this list into a data frame that contains
as rows what is now stored as sublists (columns)

The solution suggested earlier wont work here, since we have sublists
(with data of different type) instead of vectors in the list.

The result should be e.g. a data frame like:
final
   A  B
1 "x" 2
2 "y" 4

Of course I would also love to hear a completely different approach
but the requirement would be that I want to map a function over a 
list of data frames and collect multi-valued results (with different
types) into the ROWS of a new data frame.

I hesitate to do this with all sorts of loops since I love the
way of R of NOT doing it with loops.

Another issue is that I want to do this with quite a lot of
data, so it should be efficient.

Too long a question?

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