0

I am trying to sort a very large dataset that includes id numbers and timestamps. There are multiple rows with the same ID number and therefore I need the corresponding timestamps to be in datetime order. I have tried both the base order() and the dplyr arrange() functions. Example of the base order() function code:

> df[with(df, order("deviceNr", "timestamp"))]

This code seemed to remove all parts of the dataset and produced two columns of ascending numbers

Example of the dplyr arrange() function code:

 > arrange(df, "deviceNr", "timestamp")

This code arranged only by deviceNr (the ID), but not by timestamp

mrhelp
  • 1
  • 1
  • In base, you're missing the comma to show that you are ordering rows: `df[with(df, order("deviceNr", "timestamp")), ]` adding the `,` inside `[]`. – Gregor Thomas May 11 '21 at 19:50
  • @GregorThomas the quote is still not correct in `base R` – akrun May 11 '21 at 19:53
  • Can you provide a reproducible example? https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – william3031 May 12 '21 at 01:58

2 Answers2

1

We don't quoted the column names in arrange

library(dplyr)
arrange(df, deviceNr, timestamp)

Or use across which can take both quoted and unquoted

arrange(df, across(c('deviceNr', 'timestamp')))
akrun
  • 874,273
  • 37
  • 540
  • 662
1

I think you can try the code below if you still want to use order

df[with(df, order(deviceNr, timestamp)),]
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81