r语言 - 正确应用Pivot函数



我有这个数据集:

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

我的目标是将这个数据集转换成以下格式:

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

我做对了吗?

下面是我的代码:
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

谢谢!

我会这样做:

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

创建于2022-12-09与reprex v2.0.2

最新更新