How to order by date in R?

r

#1

Hi everyone,

I am new to data science and would be needing your help on an issue. I was working on Bike Sharing problem and wanted to sort my rows on the basis of date.

Actually the data set is such that data for first nineteen days of every month for 2 years is given in the training set, and the data for days from 20th of the month to the end of the month for the same 2 years is present in the test data set.
When I merged the 2 datasets using
total<-rbind(train[-c(10,11,12)],test)

the dates got shuffled up.

Now I want to order my rows such that data for 20th day comes after the 19th day of the month and so on.
Please suggest some method to implement this.

Thanks in advance!
CC


#2

Did you try order function?
Also check out arrange in dplyr package


#3

I wasn’t able to achieve the required results using them. Please correct me if I have used something wrong.

date<-arrange(total,as.numeric(total$datetime))

This did not give the required results.


#4

@Corporate_Cowboy
hey there,

can you write down the sample dataset along with the output of class(total$datetime). I may be able to help as I did this problem a few months back.

Neeraj


#5

@NSS

The class of total$datetime is “factor”.

A snapshot of the dataset canbe seen here.

For the actual data set:
train.csv (633.2 KB)


#6

@Corporate_Cowboy

hi there,

Well this is how I want you to proceed with this.

Extract year,month and date from the datetime column.

library(dplyr)
data<-arrange(total,year,month,date)

Hope it helps !!

Neeraj


#7

arrange in the library dplyr will do you work . i have used it some time back .