Search code examples
rpivottidyrdata-manipulation

Correctly Applying Pivot Functions


I have this dataset:

my_data = structure(list(col = c("A", "B", "C"), `2000-01-01` = c(86L, 
43L, 73L), `2000-01-02` = c(99L, 77L, 12L)), class = "data.frame", row.names = c(NA, 
-3L))

  col 2000-01-01 2000-01-02
1   A         86         99
2   B         43         77
3   C         73         12

My goal is to transform this dataset into the following format:

        date col count
1 2000-01-01   A    86
2 2000-01-01   B    43
3 2000-01-01   C    73
4 2000-01-02   A    99
5 2000-01-02   B    77
6 2000-01-02   C    12

Have I done this correctly?

Here is my code:

library(tidyr)
# how come this seems to works for all columns even though I only specified "2001-01-01"?
my_data %>%
  pivot_longer(!col, names_to = "2001-01-01", values_to = "count")

# A tibble: 6 x 3
  col   `2001-01-01` count
  <chr> <chr>        <int>
1 A     2000-01-01      86
2 A     2000-01-02      99
3 B     2000-01-01      43
4 B     2000-01-02      77
5 C     2000-01-01      73
6 C     2000-01-02      12

Thanks!


Solution

  • I would have done it like this:

    library(tidyr)
    library(dplyr)
    #> 
    #> Attaching package: 'dplyr'
    #> The following objects are masked from 'package:stats':
    #> 
    #>     filter, lag
    #> The following objects are masked from 'package:base':
    #> 
    #>     intersect, setdiff, setequal, union
    structure(list(col = c("A", "B", "C"), `2000-01-01` = c(86L, 
                                                            43L, 73L), `2000-01-02` = c(99L, 77L, 12L)), class = "data.frame", row.names = c(NA, 
                                                                                                                                             -3L)
        ) %>% 
        pivot_longer(-col, names_to = 'date', values_to = 'count') %>% 
        arrange(date, col)
    #> # A tibble: 6 × 3
    #>   col   date       count
    #>   <chr> <chr>      <int>
    #> 1 A     2000-01-01    86
    #> 2 B     2000-01-01    43
    #> 3 C     2000-01-01    73
    #> 4 A     2000-01-02    99
    #> 5 B     2000-01-02    77
    #> 6 C     2000-01-02    12
    

    Created on 2022-12-09 with reprex v2.0.2