0

I am trying to reshape the data I have in a data frame for longitudinal analysis. First, here's a reproducible example (albeit not the most elegant one, I am still learning):

# reproducible example
time <- c(199801, 199802)
id <- list(jan=c("Allianz", "Adidas", "Continental"), feb=c("Allianz", "Adidas", "BMW", "Continental"))
indeg <- list(jan=c(0,0,1), feb=c(0,0,2,4))
example <- data.frame(time = unique(time))
example$id <- id
example$indeg <- indeg
example$dax <- c(1,0)

As you can see, I have unique time points (time), one variable that will remain true for all observations (dax) and then some I need to somehow "unlist" (id and indeg). In reality, the data features 180 time points and a few more variables in the same format. This is what I want the data to look like later:

time2 <- c(199801, 199801, 199801, 199802, 199802, 199802, 199802)
example2 <- data.frame(time2)
example2$id <- c("Allianz", "Adidas", "Continental", "Allianz", "Adidas", "BMW", "Continental")
example2$indeg <- c(0,0,1,0,0,2,4)
example2$dax <- c(1,1,1,0,0,0,0)

I have looked into the untable and reshape function of R, but did not really understand how to use them on my data. It seems that part of the solution would be to flatten the lists by using example$id <- vapply(example$id, paste, collapse = ", ", character(1L)) before reshaping?

As I'm both new to longitudinal analysis and relatively new to R, I regularly find myself stuck with these seemingly trivial procedures. Any push in the right direction would be greatly appreciated.

I know there have been numerous questions about reshaping, but I could not find any that deal with lists in rows (perhaps because I'm not too familiar with the terminologies yet).

SteffenT
  • 119
  • 6
  • 4
    `tidyr::unnest(example)` – Sotos Aug 23 '17 at 08:49
  • Thank you, Sotos, this works perfectly fine for the example. Unfortunately it seems that my example did not do my data justice as I get the error "# Error: All nested columns must have the same number of elements." (for flattened lists) or "Each column must either be a list of vectors or a list of data frames [ID, indegree,outdegree,constraint,closeness]" for the vectors. It looks like tidyr might still be what I am looking for, so off to troubleshooting for now. – SteffenT Aug 23 '17 at 10:01
  • I got it to work using `sumarTable.unnest <- unnest(sumarTable, sumarTable$ID, sumarTable$indegree, sumarTable$outdegree, sumarTable$constraint, sumarTable$closeness, .drop = NA, .id = NULL, .sep = NULL)`. Thank you so much for your help, greatly appreciated! – SteffenT Aug 23 '17 at 10:16

0 Answers0