I need to convert a dataframe to a tibble like structure so that it can be consumed by geom_line()
(ggplot2) to create a line plot. I'm pretty sure that the solution for my question lies in dplyr and/or tidyverse but I could not figure out how to do it. I want to transform the following type of dataframe:
dates = c("2022-02-01", "2022-02-05", "2022-02-10")
test_data = data.frame(matrix(ncol = 5, nrow = 3))
colnames(test_data) = c("a", "b", "c", "d", "e")
rownames(test_data) = dates
test_data[c(1:3),] = sample(100,5,1)
to:
new_test_data = data.frame(matrix(ncol = 3, nrow = nrow(test_data) * ncol(test_data)))
colnames(new_test_data) = c("value", "date", "colname")
new_test_data[,1] = c(test_data[,1], test_data[,2], test_data[,3], test_data[,4], test_data[,5])
new_test_data[,2] = rownames(test_data)
new_test_data[,3] = c(rep(colnames(test_data)[1], 3), rep(colnames(test_data)[2], 3),rep(colnames(test_data)[3], 3), rep(colnames(test_data)[4], 3), rep(colnames(test_data)[5], 3))