-2

enter image description here

This column DATE shows covid cases for different times during the year for each country hence there is too many observation. I want to flip the DATE column horizontally so that the location stays the same and for each country there is only one row but still shows the same data.

many thanks in advance

  • 2
    Please provide a [reproducible minimal example](https://stackoverflow.com/q/5963269/8107362). Especially, provide some sample data, e.g. with `dput()`, along with data of what the solution should produce. do not use pictures! – mnist Sep 06 '21 at 21:04
  • You want e.g. pivot_wider. – deschen Sep 06 '21 at 21:13
  • @mnist sorry Its my 2nd day using stackoverflow. – Avdyl Bytyqi Sep 06 '21 at 21:14
  • Dont just say you are sorry, but please also EDIT your question. See the link provided by mnist – GuedesBF Sep 06 '21 at 22:04
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 06 '21 at 23:31
  • Does this answer your question? [Reshaping data.frame from wide to long format](https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format) – Joe Sep 13 '21 at 07:53
  • Or, https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format – Joe Sep 13 '21 at 07:54

1 Answers1

1

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))
TarJae
  • 72,363
  • 6
  • 19
  • 66