如何使用R中的sqldf搜索最后有空格的特定字符

时间:2018-02-16 00:47:36

标签: r sqldf

我有一个包含18列的数据集,并且列与报告的事故有关。我试图找到一周中特定日期报告的事故数量。例如:周二报告的事故总数。为此,我在我的数据集中使用了两个变量:Day_of_Week和number_of_vehicle。我在sqldf查询下运行。但是,它显示了整个星期内报告的事故总数。我想用sqldf报告特定的日子:星期一。我还想补充一点,Day_oF_WEEK栏中有相当多的空间。例如见下文

Day_OF_Week           Number_of_vehicle
MONDAY(Space here)              50

某些样本数据集

> dput(head(myf,5))
structure(list(CASE_NUMBER = c("1363000002", "1296000023", "1283000016", 
"1282000006", "1267000007"), BARRACK = c("Rockville", "Berlin", 
"Prince Frederick", "Leonardtown", "Essex"), ACC_DATE = c("2012-01-01T00:00:00", 
"2012-01-01T00:00:00", "2012-01-01T00:00:00", "2012-01-01T00:00:00", 
"2012-01-01T00:00:00"), ACC_TIME = c("2:01", "18:01", "7:01", 
"0:01", "1:01"), ACC_TIME_CODE = c("1", "5", "2", "1", "1"), 
    DAY_OF_WEEK = c("SUNDAY   ", "SUNDAY   ", "SUNDAY   ", "SUNDAY   ", 
    "SUNDAY   "), ROAD = c("IS 00495 CAPITAL BELTWAY", "MD 00090 OCEAN CITY EXPWY", 
    "MD 00765 MAIN ST", "MD 00944 MERVELL DEAN RD", "IS 00695 BALTO BELTWAY"
    ), INTERSECT_ROAD = c("IS 00270 EISENHOWER MEMORIAL", "CO 00220 ST MARTINS NECK RD", 
    "CO 00208 DUKE ST", "MD 00235 THREE NOTCH RD", "IS 00083 HARRISBURG EXPWY"
    ), DIST_FROM_INTERSECT = c("0", "0.25", "100", "10", "100"
    ), DIST_DIRECTION = c("U", "W", "S", "E", "S"), CITY_NAME = c("Not Applicable", 
    "Not Applicable", "Not Applicable", "Not Applicable", "Not Applicable"
    ), COUNTY_CODE = c("15", "23", "4", "18", "3"), COUNTY_NAME = c("Montgomery", 
    "Worcester", "Calvert", "St. Marys", "Baltimore"), VEHICLE_COUNT = c("2", 
    "1", "1", "1", "2"), PROP_DEST = c("YES", "YES", "YES", "YES", 
    "YES"), INJURY = c("NO", "NO", "NO", "NO", "NO"), COLLISION_WITH_1 = c("VEH", 
    "FIXED OBJ", "FIXED OBJ", "FIXED OBJ", "VEH"), COLLISION_WITH_2 = c("OTHER-COLLISION", 
    "OTHER-COLLISION", "FIXED OBJ", "OTHER-COLLISION", "OTHER-COLLISION"
    )), .Names = c("CASE_NUMBER", "BARRACK", "ACC_DATE", "ACC_TIME", 
"ACC_TIME_CODE", "DAY_OF_WEEK", "ROAD", "INTERSECT_ROAD", "DIST_FROM_INTERSECT", 
"DIST_DIRECTION", "CITY_NAME", "COUNTY_CODE", "COUNTY_NAME", 
"VEHICLE_COUNT", "PROP_DEST", "INJURY", "COLLISION_WITH_1", "COLLISION_WITH_2"
), row.names = c(NA, 5L), class = "data.frame")

sqldf代码:

sqldf("select sum(Number_of_vehicle),DAY_OF_WEEK from accident group by DAY_OF_WEEK")

任何帮助表示赞赏! 提前谢谢!

0 个答案:

没有答案