输入图片描述
这列DATE显示了每个国家一年中不同时间的covid - 19病例,因此观察太多。我想横向翻转DATE列,以便位置保持不变,每个国家只有一行,但仍然显示相同的数据。
many thanks in advance
更新:OP:新要求:见评论:现在我们可以组合pivot_longer
和pivot_wider
library(dplyr)
library(tidyr)
# set seed and randomly generate numbers for example dataframe
set.seed(123)
df %>%
mutate(total_deaths = sample.int(100, 13, replace = TRUE),
total_vaccinations = sample.int(100, 13, replace = TRUE)) %>%
pivot_longer(
cols = c(total_cases, total_deaths, total_vaccinations),
names_to = "total",
values_to = "value"
) %>%
pivot_wider(
names_from = DATE,
values_from = value
)
输出:
location total `2020-02-24` `2020-02-25` `2020-02-26` `2020-02-27` `2020-02-28` `2020-02-29` `2020-03-01` `2020-03-02` `2020-03-03` `2020-03-04` `2020-03-05` `2020-03-06` `2020-03-07`
<chr> <chr> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 Afghanistan total_cases 1 1 1 1 1 1 1 1 2 4 4 4 4
2 Afghanistan total_deaths 25 87 90 32 84 24 57 73 23 14 6 91 1
3 Afghanistan total_vaccinations 90 58 81 29 26 27 85 7 60 26 41 84 6
正如@deschen已经提到的,您需要tidyr
包中的pivot_wider
:
library(dplyr)
library(tidyr)
df %>%
pivot_wider(
names_from = DATE,
values_from = total_cases
)
输出:
location `2020-02-24` `2020-02-25` `2020-02-26` `2020-02-27` `2020-02-28` `2020-02-29` `2020-03-01` `2020-03-02` `2020-03-03` `2020-03-04` `2020-03-05` `2020-03-06` `2020-03-07`
<chr> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 Afghanistan 1 1 1 1 1 1 1 1 2 4 4 4 4
数据:
df <- structure(list(location = c("Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan"
), DATE = c("2020-02-24", "2020-02-25", "2020-02-26", "2020-02-27",
"2020-02-28", "2020-02-29", "2020-03-01", "2020-03-02", "2020-03-03",
"2020-03-04", "2020-03-05", "2020-03-06", "2020-03-07"), total_cases = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 4L, 4L, 4L, 4L)), class = "data.frame", row.names = c(NA,
-13L))