0

I have the dataframe that looks like below: There is no column like seqnum . But I need to add that column which puts sequence of the entry for a particular serial no., date and time. It resets for every day. So, for every serial no., and for day one, I need to keep track of times and add 1, 2, 3, ... and then pick another date for the same serial no. and do the same. I need to do it for all the serial numbers. I have no idea, how to acheive this. Please help. Thanks,

serial       DATE       TIME    Seqnum
12534576936 9/15/2011   7:19:00 1
12534576936 9/15/2011   7:21:00 2
12534576936 7/21/2011   7:58:00 1
12534576936 7/21/2011   8:02:00 2
12534576936 7/21/2011   6:58:00 3
12534576936 7/21/2011   7:58:00 4
12534653735 9/6/2011    6:39:00 1
12534709569 8/23/2011   7:20:00 1
user247702
  • 23,641
  • 15
  • 110
  • 157
Van
  • 13
  • 2
  • You need two tasks: learn how to format characters to R Date-class and then do sequencing within each Date and serial number. Both are answered multiple times in SO. You should do some searching. – IRTFM Apr 07 '17 at 20:50
  • @42- None of the answers in the duplicate question {http://stackoverflow.com/questions/11996135/create-a-sequential-number-counter-for-rows-within-each-group-of-a-dataframe} work on this dataframe. For example, `ave(df$DATE, df$DATE, FUN = seq_along)` complains about missing the 'origin' of the date object. – lebelinoz Apr 07 '17 at 21:10
  • I would suggest `df %>% mutate(ones = 1) %>% group_by(DATE) %>% mutate(cumsum = cumsum(ones))` works if you have the `dplyr` package. I'd break down what all the parts mean, but since the question is closed I cannot. – lebelinoz Apr 07 '17 at 21:13
  • You have not read my comment carefully. Your `DATE` is not a date or datetime. It's probably a factor, but possibly a character. Search SO on how to format character or factors with the MM/DD/YYYY format. Many answers. – IRTFM Apr 07 '17 at 23:55
  • I don't see how `ave(df$DATE, df$DATE, FUN = seq_along)` could compalin about the lack of origin. You don't have a Date-classed column. If you have code then post a reproducible example [MCVE] AND stop using comments for clarification of the question. Learn to [edit]. – IRTFM Apr 07 '17 at 23:58

0 Answers0