1

I am new in R and I've been stuck on matching hour by date for awhile now. I have this date frame that has a column for date and a column for hour. It looks like the following

Date         Hour   
June1        0            
June1        1   
June1        2    
June1        0        
June1        1    
June2        0        
June2        1

I want to be able to match all the same hour together by date. The hours are numbered from 0-23. So for example, I want all hour 1 in June 1 to be matched together and all hour 2 in June 1 to be matched (and so on). It's probably a simple solution, but I can't figure it out ): I would really appreciate some help!

user295691
  • 7,108
  • 1
  • 26
  • 35

3 Answers3

1

You can use ddply from plyr package for that:

install.packages("plyr")
library(plyr)
ddply(mydata,.(Date,Hour),transform,mean.value=mean(value)

Note: I am assuming that you want the match to find out the mean/median/sum etc of another column called as value. Also, Date need to be formatted as as.Date() before using above function.

Metrics
  • 15,172
  • 7
  • 54
  • 83
0
df <- read.table(textConnection("Date         Hour   
June1        0            
June1        1   
June1        2    
June1        0        
June1        1    
June2        0        
June2        1"), header = TRUE)

library(dplyr)
# To get counts by day
df %>% group_by(Date) %>% tally(Hour) %>% data.frame

# To group them by day and arrange by hour
df %>% arrange(Date, Hour)

It's not entirely clear what you want but from your phrasing it seems like you want the Hour arranged by Date.

Maiasaura
  • 32,226
  • 27
  • 104
  • 108
0

If I understand your question, I think you're looking to sort the array. The order function is ideal for this. Something like

> df[order(df$Date, df$Hour),]
   Date Hour
1 June1    0
4 June1    0
2 June1    1
5 June1    1
3 June1    2
6 June2    0
7 June2    1

will accomplish that.

That said, it sounds like you might not be asking the right question. It might be easier to start earlier in your data processing pipeline for how you got strings like June1 into your dataset instead of more well-formatted dates. Ideally you'd want to represent dates or datetimes using one of the built-in R classes for handling those types, to make analysis and plotting easier down the road.

user295691
  • 7,108
  • 1
  • 26
  • 35