I've never posted before so if I'm in the wrong place or am providing the wrong information please just let me know.
I love dplyr/tidyr and want to convert new work and some old work to it, from plyr/reshape2. The question below is in that spirit: I know how to do it well in plyr, and have figured out how to do it unattractively with dplyr, but I want to know if there is a way to do it well with dplyr.
I want to be able to convert a list of data to a data frame, where each list element is, conceptually, an observation in a data set. A very practical application is when data returned from an API, after conversion from JSON, is in list form like this. I can do this easily with ldply but my dplyr solution is ugly. My question is, is there a better way to do it with dplyr/tidyr?
Here is a reproducible example
# Create a list of data, where each element is an observation
obs1 <- list(x="a", value=123)
obs2 <- list(x="b", value=27)
obs3 <- list(x="c", value=99)
dlist <- list(obs1, obs2, obs3)
dlist
# I want to convert this into a data frame that looks like
goal <- data.frame(x=c("a", "b", "c"), value=c(123, 27, 99))
goal
# In the days of plyr I would do this by
df1 <- ldply(dlist, data.frame)
df1
# Here is how I have figured out how to do it with dplyr. (It produces rbind_all warnings and slight differences in column types, but neither concerns me.)
# No self-respecting analyst should accept a solution like this. This CAN'T be the best dplyr solution, can it?
df2 <- data.frame(recs=1:length(dlist)) %>%
group_by(recs) %>%
do(data.frame(t(unlist(dlist[.$recs])))) %>%
ungroup() %>% select(-recs)
df2
Again, I know I can just use ldply, but I am trying to use dplyr as well as possible, and I am trying to convert to dplyr/tidyr as much as possible. Many thanks if you have a better way.
Don