0

I have a dataset includes all the countries, and multiple date columns which contains number of people being confirmed to have disease. like this

country     1/1   1/2   1/3  1/4 .......   1/31
   A         2     4     0    5             10
   B         3     3     5    1             2
   C         4     2     6    8             3

enter image description here

How can I convert it to be like this:

enter image description here

 country    date     number of confirmed
   A        1/1              2
   A        1/2              4
   A        1/3              0
   A        1/4              5
   ..........................
   C        1/31             3

Using R studio or Python. It's really hard to use Excel manually change this, but I don't know how to achieve this using python or R.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
kiki
  • 1
  • 1
  • 1
    Please post data as text that is formatted and copy/pasteable, not pictures. It's very hard to work with pictures of data. – Gregor Thomas Apr 28 '20 at 16:33
  • See [Discourage screenshots of code and/or errors](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors) – martineau Apr 28 '20 at 16:33
  • For R, you can see the FAQ on [reshaping data from wide to long formats](https://stackoverflow.com/q/2185252/903061) for a variety of methods. – Gregor Thomas Apr 28 '20 at 16:33

2 Answers2

0

In R you can use pivot_longer from dplyr

library(readr)
df <- read_table("
country     1/1   1/2   1/3  1/4
A           2     4     0    5
B           3     3     5    1
C           4     2     6    8"
)


library(dplyr)
df %>% 
  pivot_longer(cols = -country, names_to = "date", values_to = "no of confirmed")
# A tibble: 12 x 3
   country date  `no of confirmed`
   <chr>   <chr>             <dbl>
 1 A       1/1                   2
 2 A       1/2                   4
 3 A       1/3                   0
 4 A       1/4                   5
 5 B       1/1                   3
 6 B       1/2                   3
 7 B       1/3                   5
 8 B       1/4                   1
 9 C       1/1                   4
10 C       1/2                   2
11 C       1/3                   6
12 C       1/4                   8
Greg
  • 3,570
  • 5
  • 18
  • 31
0

You can load your dataset into a pandas DataFrame and use the pandas' pivot function see here (Python)

paul_omah
  • 1
  • 1