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).