我在R中有这样的数据框..
cambridgeparking <- read.csv("camparking.csv")
Ticket.Issue.Date Issue.Time Location Violation.Description
January 1, 2014 1:24AM Green ST NO PARKING
January 2, 2014 10:45AM Gore ST NO STOPPING
January 6, 2014 3:00PM Franklin ST HANDICAP RAMP
.. .. .. ..
March 9, 2014 3:29AM Green ST NO PARKING
.. .. .. ..
November 10, 2014 9:30PM Marshall ST METER PASSED
November 30, 2014 10:20PM Erie ST METER PASSED
如何计算每月发生的每次违规行为,以便这样做 它会显示每个月的违规总数吗?
我试图通过使用
来计算它janparking <- subset(camparking, Ticket.Issue.Date=="January 1, 2014")
但是,它仅计算2014年1月1日期间发生的违规行为。
答案 0 :(得分:2)
我们可以通过使用 library(caret)
set.seed(1)
cTune <- train(x = trainingPredictors, y = trainingOutcome,
"cubist",
tuneGrid = expand.grid(.committees = c(1, 10, 50, 100),
.neighbors = c(0, 1, 5, 9)),
trControl = trainControl(method = "cv"))
cTune
删除月份之后的部分字符串,从'Ticket.Issue.Date'中提取月份,然后使用sub
获取每月的计数。
table
如果我们只需要'1月'
table(sub(' .*', '', camparking$Ticket.Issue.Date))
# January March November
# 3 1 2
sum(sub(' .*', '', camparking$Ticket.Issue.Date)=='January')
#[1] 3