Update: OP: New requirement: see comments:
Now we could combine pivot_longer
and 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
)
output:
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
As @deschen already mentioned you need pivot_wider
from tidyr
package:
library(dplyr)
library(tidyr)
df %>%
pivot_wider(
names_from = DATE,
values_from = total_cases
)
Output:
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
data:
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))