您可以有幾種方法做到這一點,但我懷疑最簡單的方法是使用table
。使用「表格」,您可以返回日期的頻率。這基本上只是數據框中日期的計數。
您可以在提取小時後做同樣的事情 - 甚至可以通過執行table(DF$DATE,DF$HOUR)
來嵌套它。使用as.data.frame
會讓你的上市有點像你正在尋找的東西。
編輯添加:爲迴應您對問題的編輯,您可以使用factor
級別獲得table
聲明中的零級別。 table
通過將它們包括在輸出中來尊重你的因素水平,即使它在輸入中找不到(實際上,我認爲table
強制輸入到背面的因素中)。
示例代碼:
# Set options and load example data
options(stringsAsFactors = FALSE)
date.data <- data.frame(DATE = c("2014-02-15","2014-02-15","2014-04-15","2014-05-15","2014-06-15","2014-06-15"),
TIME = c("15:02","15:12","02:02","11:02","15:42","16:02"))
# Extract the hour
date.data$HOUR <- sapply(X = strsplit(x = date.data$TIME,split = ":"),FUN = `[[`,1)
# Now, set the hours as a factor level - this will allow table() to fill the data in as you are requesting
date.data$HOUR <- factor(x = date.data$HOUR,
levels = c("00","01","02","03","04","05",
"06","07","08","09","10","11",
"12","13","14","15","16","17",
"18","19","20","21","22","23"),
labels = c("00","01","02","03","04","05",
"06","07","08","09","10","11",
"12","13","14","15","16","17",
"18","19","20","21","22","23"))
# Obtain the first table of interest
as.data.frame(table(date.data$DATE))
Var1 Freq
1 2014-02-15 2
2 2014-04-15 1
3 2014-05-15 1
4 2014-06-15 2
# And the second table
as.data.frame(table(date.data$DATE,date.data$HOUR))
Var1 Var2 Freq
1 2014-02-15 00 0
2 2014-04-15 00 0
3 2014-05-15 00 0
4 2014-06-15 00 0
5 2014-02-15 01 0
6 2014-04-15 01 0
7 2014-05-15 01 0
8 2014-06-15 01 0
....
您剛剛編輯的第二部分是有點複雜。 – TARehman