Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order dataframe chronologically based on dates which are formatted %d/%m/%Y

I have some data that has to be formatted as (%d/%m/%Y). The data is out of chronological order because it is sorted by the first number which is the day, not the month.

I'm hoping I can specify to order or reorder that I want the sorting to happen differently. I'm just not sure how to do this.

Here is some date data to be ordered:

date
1/1/2009  
1/1/2010
1/1/2011
5/4/2009
5/4/2011
10/2/2009
10/3/2011
15/9/2010
15/3/2009
31/12/2011
31/7/2009

Thanks for any suggestions.

like image 573
Nazer Avatar asked Jul 25 '13 17:07

Nazer


People also ask

How do I order a DataFrame in R by date?

Use setorder() function from data. table to perform sorting on date column. This function takes the data. frame object and column as input and return a new DataFrame after sorting by the specified column (date).

How do you sort a DataFrame based on a date column in Python?

Python. We will be using the sort_values() method to sort our dataset and the attribute that we will pass inside the function is the column name using which we want to sort our DataFrame.

How do you sort data frame by date?

“One of the tangible ways to sort the date column of the DataFrame is with a pandas sort by date. This algorithm can sort a DataFrame's single and multiple date columns. The “to DateTime()” and “sort values()” functions are the two main functions used by pandas to sort by date.

How do you put dates in order in R?

Method 1: User order() from base R Here order() function is used to sort the dataframe by R using order() function based on the date column, we have to convert the date column to date with the format, this will sort in ascending order.


1 Answers

When order by column date convert it Date format.

df[order(as.Date(df$date,format="%d/%m/%Y")),,drop=FALSE]
         date
1    1/1/2009
6   10/2/2009
9   15/3/2009
4    5/4/2009
11  31/7/2009
2    1/1/2010
8   15/9/2010
3    1/1/2011
7   10/3/2011
5    5/4/2011
10 31/12/2011
like image 126
Didzis Elferts Avatar answered Nov 15 '22 21:11

Didzis Elferts