如何使用tapply匹配特定条件

时间:2018-02-18 23:23:04

标签: r tapply

我有事故数据集,其中包含报告的事故数量。我正在尝试使用tapply功能,这将显示报告的事故总数"星期四"。但是,而不是返回特定日期报告的事故数量。它显示了我的数据集中的总行数。我正在tapply(myfinal$VEHICLE_COUNT,myfinal$DAY_OF_WEEK=='THURSDAY',length) 函数下面使用。:

> dput(tail(myfinal,5))
structure(list(CASE_NUMBER = c("1251045636", "1251045630", "1251045591", 
"1251045574", "1250010434"), BARRACK = c("Frederick", "Frederick", 
"Frederick", "Frederick", "Jessup"), ACC_DATE = c("2012-12-31T00:00:00", 
"2012-12-31T00:00:00", "2012-12-31T00:00:00", "2012-12-31T00:00:00", 
"2012-12-31T00:00:00"), ACC_TIME = c("18:12", "18:12", "12:12", 
"9:12", "11:12"), ACC_TIME_CODE = c("5", "5", "4", "3", "3"), 
    DAY_OF_WEEK = c("MONDAY   ", "MONDAY   ", "MONDAY   ", "MONDAY   ", 
    "MONDAY   "), ROAD = c("IS 00070 EISENHOWER MEMOR HWY", "MD 00077 ROCKY RIDGE RD", 
    "MD 00085 BUCKEYSTOWN PIKE", "MD 00017 MYERSVILLE RD", "IS 00070 No Name"
    ), INTERSECT_ROAD = c("CO 00248 MONUMENT RD", "MD 00076 MOTTERS STATION RD", 
    "CO 00308 MANOR WOODS RD", "CO 00941 DAWN CT", "US 00029 Columbia Pike"
    ), DIST_FROM_INTERSECT = c("300", "0", "400", "500", "0.25"
    ), DIST_DIRECTION = c("E", "U", "S", "S", "E"), CITY_NAME = c("Not Applicable", 
    "Not Applicable", "Not Applicable", "Not Applicable", NA), 
    COUNTY_CODE = c("10", "10", "10", "10", "13"), COUNTY_NAME = c("Frederick", 
    "Frederick", "Frederick", "Frederick", "Howard"), VEHICLE_COUNT = c(1, 
    2, 2, 1, 2), PROP_DEST = c("NO", "YES", "YES", "NO", "NO"
    ), INJURY = c("YES", "NO", "NO", "YES", "YES"), COLLISION_WITH_1 = c("FIXED OBJ", 
    "VEH", "VEH", "NON-COLLISION", "VEH"), COLLISION_WITH_2 = c("OTHER-COLLISION", 
    "OTHER-COLLISION", "OTHER-COLLISION", "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 = 18634:18638, class = "data.frame")

我的样本数据集如下:

print('');

有关如何修复它的任何建议!提前谢谢!

3 个答案:

答案 0 :(得分:2)

如果只能出于任何原因使用tapply,那么,建立Maurits'回答,你应该能够做到这一点:

tapply(myfinal$VEHICLE_COUNT,trimws(myfinal$DAY_OF_WEEK)=='THURSDAY',length)

或类似的。似乎DAY_OF_WEEK变量中的字符串最后有很多空格。您需要删除它们(通过trimws)或修改比较字符串以包含这些空格(例如myfinal$DAY_OF_WEEK=="THURSDAY ")。使用比较运算符,只有匹配两个字符串才能匹配两个字符串,因此任何一个字符串中的任何其他空格都会对您不利。

答案 1 :(得分:1)

在这里使用tapply真的没有意义!

方法1

使用dplyr

require(tidyverse);
df %>% filter(trimws(DAY_OF_WEEK) == "MONDAY") %>% summarise(count = n());
#  count
#1     5

方法2

在基数R中,使用subsettable

table(subset(df, trimws(DAY_OF_WEEK) == "MONDAY")$DAY_OF_WEEK);
#MONDAY
#    5

我在这里使用了“星期一”,因为你没有DAY_OF_WEEK = "THURSDAY"的条目。

样本数据

df <- structure(list(CASE_NUMBER = c("1251045636", "1251045630", "1251045591",
"1251045574", "1250010434"), BARRACK = c("Frederick", "Frederick",
"Frederick", "Frederick", "Jessup"), ACC_DATE = c("2012-12-31T00:00:00",
"2012-12-31T00:00:00", "2012-12-31T00:00:00", "2012-12-31T00:00:00",
"2012-12-31T00:00:00"), ACC_TIME = c("18:12", "18:12", "12:12",
"9:12", "11:12"), ACC_TIME_CODE = c("5", "5", "4", "3", "3"),
    DAY_OF_WEEK = c("MONDAY   ", "MONDAY   ", "MONDAY   ", "MONDAY   ",
    "MONDAY   "), ROAD = c("IS 00070 EISENHOWER MEMOR HWY", "MD 00077 ROCKY RIDGE RD",
    "MD 00085 BUCKEYSTOWN PIKE", "MD 00017 MYERSVILLE RD", "IS 00070 No Name"
    ), INTERSECT_ROAD = c("CO 00248 MONUMENT RD", "MD 00076 MOTTERS STATION RD",
    "CO 00308 MANOR WOODS RD", "CO 00941 DAWN CT", "US 00029 Columbia Pike"
    ), DIST_FROM_INTERSECT = c("300", "0", "400", "500", "0.25"
    ), DIST_DIRECTION = c("E", "U", "S", "S", "E"), CITY_NAME = c("Not Applicable",
    "Not Applicable", "Not Applicable", "Not Applicable", NA),
    COUNTY_CODE = c("10", "10", "10", "10", "13"), COUNTY_NAME = c("Frederick",
    "Frederick", "Frederick", "Frederick", "Howard"), VEHICLE_COUNT = c(1,
    2, 2, 1, 2), PROP_DEST = c("NO", "YES", "YES", "NO", "NO"
    ), INJURY = c("YES", "NO", "NO", "YES", "YES"), COLLISION_WITH_1 = c("FIXED OBJ",
    "VEH", "VEH", "NON-COLLISION", "VEH"), COLLISION_WITH_2 = c("OTHER-COLLISION",
    "OTHER-COLLISION", "OTHER-COLLISION", "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 = 18634:18638, class = "data.frame")

答案 2 :(得分:1)

基础R解决方案是通过&#34; THURSDAY&#34;来分组DAY_OF_WEEK。然后返回行数:

nrow(df[df$DAY_OF_WEEK == "THURSDAY",])