0

I need to sort the dataframe based on time, in time stamp column.

Here is my dput input:

structure(list(Time.stamp = structure(1:6, .Label = c("05/06/2016 21:13", 
"05/06/2016 22:52", "05/06/2016 22:58", "09/06/2016 22:40", "09/06/2016 22:45", 
"09/06/2016 22:50"), class = "factor")), .Names = "Time.stamp", class = "data.frame", row.names = c(NA, 
-6L))

My desired output is

Expected
05/06/2016            21:13
09/06/2016            22:40
09/06/2016            22:45
09/06/2016            22:50
05/06/2016            22:52
05/06/2016            22:58

I tried lubricate package,

data = dmy_hm(data$Time.stamp)   
data2 = arrange(data,f_device_time_new)

But it doesnt work. Can anyone provide some suggestion?

nicola
  • 24,005
  • 3
  • 35
  • 56
  • 1
    I don't understand your expected output. There appears to be no logic as to the sorting by day, only the hour. Do you have a typo here? – Tim Biegeleisen May 18 '18 at 11:02
  • Possible duplicate of [R help converting factor to date](https://stackoverflow.com/questions/17496358/r-help-converting-factor-to-date) – pieca May 18 '18 at 11:02
  • Here time column mismatch and occurs randomly, it needs to sorted based on time in the timestamp column. Please let me if you still dont understand. This column in already as factor. But I need to sort it – Krishna Moorthy May 18 '18 at 11:04

1 Answers1

1

One possible solution is this:

data = structure(list(Time.stamp = structure(1:6, .Label = c("05/06/2016 21:13", 
"05/06/2016 22:52", "05/06/2016 22:58", "09/06/2016 22:40", "09/06/2016 22:45", 
"09/06/2016 22:50"), class = "factor")), .Names = "Time.stamp", class = "data.frame", row.names = c(NA, 
-6L))

library(dplyr)
library(lubridate)

data %>%
  mutate(Time.stamp = dmy_hm(Time.stamp),
         hour = hour(Time.stamp),
         min = minute(Time.stamp),
         sec = second(Time.stamp)) %>%
  arrange(hour, min, sec) %>%
  select(Time.stamp)

#   Time.stamp
# 1 2016-06-05 21:13:00
# 2 2016-06-09 22:40:00
# 3 2016-06-09 22:45:00
# 4 2016-06-09 22:50:00
# 5 2016-06-05 22:52:00
# 6 2016-06-05 22:58:00

Note that in your case you don't need the sec column, but I'm posting a more general solution.

AntoniosK
  • 15,991
  • 2
  • 19
  • 32
  • Hi @AntoniosK, I am not getting time in that column, I am getting only date as output. Is there anything I missed – Krishna Moorthy May 18 '18 at 11:19
  • Are you using exactly the dataset you posted as an example, or your full dataset? – AntoniosK May 18 '18 at 11:22
  • I am using full dataset. – Krishna Moorthy May 18 '18 at 11:28
  • If you can run my solution fine, but you have a problem with your full dataset, then we're missing something. Column name? Column type? Not sure. Maybe you have to post a more representative example of your dataset. – AntoniosK May 18 '18 at 11:38
  • since my dataset is huge, I cannot post it here, But here is my output: "2016-04-01" "2016-04-02" , Printing data continously instead of time. Here is my code: data1 = data %>% mutate(f_device_time = dmy_hm(f_device_time), hour = hour(f_device_time), min = minute(f_device_time)) %>% arrange(hour,min) %>% select(f_device_time) – Krishna Moorthy May 18 '18 at 11:54
  • Maybe you can get a random sample of 10 rows of your dataset and use the dput. In this way we can keep your original column names and types. – AntoniosK May 18 '18 at 12:04
  • Your solution is fine. It is running perfectly, but my dataset is not working properly, any particular reason for that? – Krishna Moorthy May 18 '18 at 12:04
  • since I am new I dont how to use random sample of 10. Can you let me know? – Krishna Moorthy May 18 '18 at 12:06