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!

4

3 回答 3

1

您可以使用ddplyplyr 包:

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

注意:我假设您希望匹配找出名为的另一列的均值/中值/总和等value。此外,Date需要像as.Date()使用上述功能之前一样进行格式化。

于 2013-08-09T20:37:44.710 回答
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)

尚不完全清楚您想要什么,但从您的措辞看来您想要按日期排列的小时。

于 2015-09-15T17:48:08.927 回答
0

如果我理解您的问题,我认为您正在寻找对数组进行排序。该order功能非常适合这一点。就像是

> 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

将实现这一点。

也就是说,听起来你可能没有问正确的问题。在数据处理管道中较早开始可能更容易将字符串像June1输入数据集而不是格式更正确的日期。理想情况下,您希望使用用于处理这些类型的内置 R 类之一来表示日期或日期时间,以使分析和绘图更容易。

于 2015-09-15T17:48:18.237 回答