0

I have a csv which is has a list of lat-long coordinates with dates for each. I have code to split the data by date

X <- split(mydata, mydata$date)

But when I run the function the data is sorted numerically rather than by date. So for example this:

  long       lat       date
 26.71360 -31.67479 04-04-2013
 26.53347 -31.54144 05-04-2013
 26.36730 -31.39950 06-04-2013
 26.15438 -31.27452 04-05-2013
 26.25447 -31.06523 04-05-2013
 26.31591 -30.92225 04-05-2013

Would be converted to this: Note that dates are d/m/y

long       lat       date
 26.71360 -31.67479 04-04-2013
 26.53347 -31.54144 04-05-2013
 26.36730 -31.39950 04-05-2013
 26.15438 -31.27452 04-05-2013
 26.25447 -31.06523 05-04-2013
 26.31591 -30.92225 06-04-2013

How can I keep the order?

Thanks

adkane
  • 1,429
  • 14
  • 29
  • 1
    Can you provide a reproducible example? – Roman Luštrik Mar 02 '14 at 11:01
  • Please check these links about general ideas of a reproducible example and how to producce one in R: [**here**](http://stackoverflow.com/help/mcve) and [**here**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) – Henrik Mar 02 '14 at 11:04

1 Answers1

1

You need to make sure that your 'date' variable is of class Date. Conversion from character to Date representations of dates can be made with as.Date. Because the format of your input dates is neither "%Y-%m-%d" nor "%Y/%m/%d" (see format argument in ?as.Date) you need to specify format explicitly. See ?strptime for abbreviations of the various time components.

df$date <- as.Date(df$date, format = "%d-%m-%Y")
Henrik
  • 65,555
  • 14
  • 143
  • 159