转换日期数据框
Transform Date Dataframe
我有一个看起来像这样的数据框:
County 2020-01-22 2020-01-23 2020-01-24
Autauga 0 1 0
Baldwin 0 2 4
Barbour 0 3 1
但是,我想重新格式化它,以便按县计算每天的计数。像这样:
County year month day value
Autauga 2020 01 22 0
Baldwin 2020 01 22 0
Barbour 2020 01 22 0
Autauga 2020 01 23 1
Baldwin 2020 01 23 2
Barbour 2020 01 23 3
Autauga 2020 01 24 0
Baldwin 2020 01 24 4
Barbour 2020 01 24 1
这个有用吗:
library(dplyr)
library(tidyr)
df %>% pivot_longer(!County, names_to = 'date') %>%
separate(date, into = c('year','month','day'), sep = '-') %>% arrange(day)
# A tibble: 9 x 5
County year month day value
<chr> <chr> <chr> <chr> <dbl>
1 Autauga 2020 01 22 0
2 Baldwin 2020 01 22 0
3 Barbour 2020 01 22 0
4 Autauga 2020 01 23 1
5 Baldwin 2020 01 23 2
6 Barbour 2020 01 23 3
7 Autauga 2020 01 24 0
8 Baldwin 2020 01 24 4
9 Barbour 2020 01 24 1
我们可以只使用 pivot_longer
并指定 names_sep
library(tidyr)
pivot_longer(df1, cols = -County,
names_to = c("year", "month", "day"), names_sep = "-")
-输出
# A tibble: 9 x 5
# County year month day value
# <chr> <chr> <chr> <chr> <int>
#1 Autauga 2020 01 22 0
#2 Autauga 2020 01 23 1
#3 Autauga 2020 01 24 0
#4 Baldwin 2020 01 22 0
#5 Baldwin 2020 01 23 2
#6 Baldwin 2020 01 24 4
#7 Barbour 2020 01 22 0
#8 Barbour 2020 01 23 3
#9 Barbour 2020 01 24 1
数据
df1 <- structure(list(County = c("Autauga", "Baldwin", "Barbour"),
`2020-01-22` = c(0L,
0L, 0L), `2020-01-23` = 1:3, `2020-01-24` = c(0L, 4L, 1L)),
class = "data.frame", row.names = c(NA,
-3L))
我有一个看起来像这样的数据框:
County 2020-01-22 2020-01-23 2020-01-24
Autauga 0 1 0
Baldwin 0 2 4
Barbour 0 3 1
但是,我想重新格式化它,以便按县计算每天的计数。像这样:
County year month day value
Autauga 2020 01 22 0
Baldwin 2020 01 22 0
Barbour 2020 01 22 0
Autauga 2020 01 23 1
Baldwin 2020 01 23 2
Barbour 2020 01 23 3
Autauga 2020 01 24 0
Baldwin 2020 01 24 4
Barbour 2020 01 24 1
这个有用吗:
library(dplyr)
library(tidyr)
df %>% pivot_longer(!County, names_to = 'date') %>%
separate(date, into = c('year','month','day'), sep = '-') %>% arrange(day)
# A tibble: 9 x 5
County year month day value
<chr> <chr> <chr> <chr> <dbl>
1 Autauga 2020 01 22 0
2 Baldwin 2020 01 22 0
3 Barbour 2020 01 22 0
4 Autauga 2020 01 23 1
5 Baldwin 2020 01 23 2
6 Barbour 2020 01 23 3
7 Autauga 2020 01 24 0
8 Baldwin 2020 01 24 4
9 Barbour 2020 01 24 1
我们可以只使用 pivot_longer
并指定 names_sep
library(tidyr)
pivot_longer(df1, cols = -County,
names_to = c("year", "month", "day"), names_sep = "-")
-输出
# A tibble: 9 x 5
# County year month day value
# <chr> <chr> <chr> <chr> <int>
#1 Autauga 2020 01 22 0
#2 Autauga 2020 01 23 1
#3 Autauga 2020 01 24 0
#4 Baldwin 2020 01 22 0
#5 Baldwin 2020 01 23 2
#6 Baldwin 2020 01 24 4
#7 Barbour 2020 01 22 0
#8 Barbour 2020 01 23 3
#9 Barbour 2020 01 24 1
数据
df1 <- structure(list(County = c("Autauga", "Baldwin", "Barbour"),
`2020-01-22` = c(0L,
0L, 0L), `2020-01-23` = 1:3, `2020-01-24` = c(0L, 4L, 1L)),
class = "data.frame", row.names = c(NA,
-3L))