1

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))
Tobias
  • 564
  • 3
  • 13
  • 1
    Are you looking for [this](https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format)? – Rui Barradas Feb 17 '22 at 12:40

1 Answers1

2

You can do:

library(dplyr)
library(tibble)
library(tidyr)

test_data %>% 
  rownames_to_column() %>%
  pivot_longer(-1)
#> # A tibble: 15 x 3
#>    rowname    name  value
#>    <chr>      <chr> <int>
#>  1 2022-02-01 a         5
#>  2 2022-02-01 b         7
#>  3 2022-02-01 c        21
#>  4 2022-02-01 d       100
#>  5 2022-02-01 e        87
#>  6 2022-02-05 a        21
#>  7 2022-02-05 b       100
#>  8 2022-02-05 c        87
#>  9 2022-02-05 d         5
#> 10 2022-02-05 e         7
#> 11 2022-02-10 a        87
#> 12 2022-02-10 b         5
#> 13 2022-02-10 c         7
#> 14 2022-02-10 d        21
#> 15 2022-02-10 e       100
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87