Split Dataframe into several

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Split Dataframe into several

javad bayat
Dear R users;
Hi.
I have a problem with splitting dataframe into several.
I have a large dataframe (Length of 61000). It has 4 Columns as below:
"
               Date             Cases          Country
1            2020-12-14     746            Country1
2            2020-12-15      324           Country1
..
6000      2020-12-13      298            Country2
.
.
"
Each country has a different number of rows and their related dates are
different. What I want to do is: Extract rows for each country and make a
new column for that case of the country.
"
             Date             Cases_Country1      Cases_Country2
 Cases_Country3 ......
1           2020-12-14     746                      25
                65
2           2020-12-15      324
..
6000      2020-12-13      298                      352
              75
"
I used split function, but it create a list and I cannot use the data.
Please help me to fix this problem.
Sincerely



--
Best Regards
Javad Bayat
M.Sc. Environment Engineering
Alternative Mail: [hidden email]

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Split Dataframe into several

Rasmus Liland-3
Dear Javad,

        data <- "Date Cases Country
        2020-12-14 746 Country1
        2020-12-15 324 Country1
        2020-12-15   1 Country3
        2020-12-13 298 Country2"
        data <- read.table(text=data, header=T)
        x <- reshape(
          data=data,
          timevar = "Country",
          idvar = "Date",
          direction = "wide")
        data
        x

yields

                Date Cases  Country
        1 2020-12-14   746 Country1
        2 2020-12-15   324 Country1
        3 2020-12-15     1 Country3
        4 2020-12-13   298 Country2
                Date Cases.Country1
        1 2020-12-14            746
        2 2020-12-15            324
        4 2020-12-13             NA
          Cases.Country3 Cases.Country2
        1             NA             NA
        2              1             NA
        4             NA            298

Best,
Rasmus

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Split Dataframe into several

Rasmus Liland-3
On 2021-03-08 17:14 +0330, javad bayat wrote:
> Dear Rasmus;
> Many thanks. It works for me.
> Sincerely yours

Great :)

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

signature.asc (849 bytes) Download Attachment