0
votes

My aim is to re-order the rows of my dataframe following the progression of months (from December, November, October, etc. to January, February, March, etc.) for each year.

At the moment I have the following dataframe:

            Date            Title  Article
1   1 December 2000 PRESS CONFERENCE Article1
2   1 November 2000 PRESS CONFERENCE Article2
3    1 October 2000 PRESS CONFERENCE Article3
4  1 September 2000 PRESS CONFERENCE Article1
5     1 August 2000 PRESS CONFERENCE Article2
6       1 July 2000 PRESS CONFERENCE Article3
7       1 June 2000 PRESS CONFERENCE Article1
8             1 May PRESS CONFERENCE Article2
9      1 April 2000 PRESS CONFERENCE Article3
10     1 March 2000 PRESS CONFERENCE Article1
11  1 February 2000 PRESS CONFERENCE Article2
12   1 January 2000 PRESS CONFERENCE Article3
13   1 December 2001 PRESS CONFERENCE Article1
14   1 November 2001 PRESS CONFERENCE Article2
15   1 October 2001 PRESS CONFERENCE Article3
.
.
.
n    1 January 2001 PRESS CONFERENCE Article

This kind of structure holds from 2000 to 2019. Now, I want to reverse it and get something like this for each year from 2000 to 2019:

Date            Title  Article
1   1 January 2000 PRESS CONFERENCE Article1
2   1 February 2000 PRESS CONFERENCE Article2
3    1 March 2000 PRESS CONFERENCE Article3
4  1 April 2000 PRESS CONFERENCE Article1
5     1 May 2000 PRESS CONFERENCE Article2
6       1 June 2000 PRESS CONFERENCE Article3
7       1 July 2000 PRESS CONFERENCE Article1
8       1 August 2000 PRESS CONFERENCE Article2
9      1 September 2000 PRESS CONFERENCE Article3
10     1 October 2000 PRESS CONFERENCE Article1
11  1 November 2000 PRESS CONFERENCE Article2
12   1 December 2000 PRESS CONFERENCE Article3
13   1 January 2001 PRESS CONFERENCE Article1
14   1 February 2001 PRESS CONFERENCE Article2
15   1 March 2001 PRESS CONFERENCE Article3
.
.
.
n    1 December 2001 PRESS CONFERENCE Article

Can anyone help me with that? How can I do it?

Thanks a lot!

2

2 Answers

2
votes

Transfer Date to the right format then use arrange from dplyr

library(dplyr)
df %>% arrange(lubridate::dmy(Date))

             Date Title
1  1 October 2000 PRESS
2 1 November 2000 PRESS
3 1 December 2000 PRESS
4  1 October 2001 PRESS
5 1 November 2001 PRESS
6 1 December 2001 PRESS

Using base R

df[order(as.Date(df$Date, "%d %B %Y")),]

Data

#Providing small dataset using `dput` will make it easier for other to help
df <- structure(list(Date = c("1 December 2000", "1 November 2000", 
"1 October 2000", "1 December 2001", "1 November 2001", "1 October 2001"
), Title = c("PRESS", "PRESS", "PRESS", "PRESS", "PRESS", "PRESS"
)), class = "data.frame", row.names = c("1", "2", "3", "13", 
"14", "15"))
2
votes

We can use anydate from anytime

library(dplyr)
library(anytime)
df %>%
    arrange(anydate(Date))