數據:
data <- read.table(text = "Col1 Col2 Col3
hey hi 'july 12,2013'
hey hi 'june 12,2013'
hey hi 'April 12,2013'
hey hi 'April 14,2012'", header = TRUE)
的回答使用日期:
#tranform data in POSIXlt
data$Col3 <- as.POSIXlt(data$Col3, format="%B %d, %Y")
## group using table with POSIXlt numbers (0 is january)
table(data$Col3$mon)
3 5 6
2 1 1
## group using table with normal month numbers
table(month(data$Col3))
4 6 7
2 1 1
## group using aggregate with POSIXlt numbers (0 is january)
aggregate(data$Col1, by=list(data[,"Col3"]$mon), length)
#result
Group.1 x
1 3 2
2 5 1
3 6 1
## group using aggregate with normal month numbers
aggregate(data$Col1, by=list(month(data$Col3)), length)
#result
Group.1 x
1 4 2
2 6 1
3 7 1
PS:磨片,你得到$ COL3 $週一在POSIXlt月份數據爲0,所以四月是3而不是4你會期待。要獲得「正常」的月份數字,您應該使用月份(數據$ Col3) - 剛剛意識到閱讀Ananda的評論。
如果你想有一個更漂亮的版本(由阿難Mahto):
Col3 <- as.POSIXlt(data$Col3, format="%B %d, %Y"); table(month.name[month(Col3)])
April July June
2 1 1
嗯嘗試這個時候你在哪裏卡住? – lukeA
而不是'regexp'如何用'month'來計算,即使用Date函數? –