3

I have a reoccuring problem - I apologize!

Say I want to have the baseball data (from the plyr package) listed according to 'id' and 'year'. There is a difference between creating the list according to either:

1. mylist1 <- dlply(baseball, .(id, year), identity)

and

2. mylist2 <- dlply(baseball, .(id), dlply, .(year), identity)

in the way the list is organized, but getting the list back into a data frame is working fine with 'mylist1'.

mydf1 <- ldply(mylist1)

but not with 'mylist2'

mydf2 <- ldply(mylist2)

which gives the following error message:

Error in list_to_dataframe(res, attr(.data, "split_label")): Result must be all atomic, or all data frames

I am a newbie to R, and this error message doesn't make much sense to me.

I would like to split my own data frame according to method 2, since I need quite a bit of data manipulation. My question is: how can I merge this list into a data frame? Is there an alternative to do.call(rbind, do.call(rbind,...?

I am greatful for any help!

3
  • 1
    Why do you need to analyse the data in this way? I don't think I've ever had to do something like this. Maybe it's just because in designing solutions to problems I try to avoid getting data structures that I can't assemble again. My preferred way of working is to stick to data.frames whenever possible, i.e. use ddply by preference.
    – Andrie
    Oct 14, 2011 at 16:42
  • You might be right, but I work with hourly accumulated data of a meteorological data. I have a time series for each station, member of an ensemble, date and forecast time, and for each time series I need to get hourly values - not accumulated values. Sisse
    – Sisse
    Oct 19, 2011 at 10:08
  • That is possible using ddply. Without knowing your data structure it's bit hard to double guess, but I suspect that combining all of your data into one massive data.frame and ensuring you have the right indices should work. Good luck with your analysis.
    – Andrie
    Oct 19, 2011 at 10:16

1 Answer 1

5

I agree with @Andrie that this is an odd structure. But I assume that you have a particular reason for doing it this way.

Since it took two passes with dlply to create mylist2, it takes two invocations of ldply to put it back together.

mydf2 <- ldply(mylist2, ldply)

This restores baseball (modulo ordering)

> class(mydf2)
[1] "data.frame"
> all(dim(mydf2) == dim(baseball))
[1] TRUE
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.