这是一个小数据框:
new_incidents <-
structure(list(id = c(18304380L, 18304383L, 18304385L, 18304388L,
18304390L, 18304392L), crime_type = c("COMMON ASSAULT", "AGGRAV ASSAULT",
"INVESTIGATE", "DISORDERLY", "COMMON ASSAULT", "COMMON ASSAULT"
), incident_date = c("9/1/17", "9/1/17", "9/1/17", "9/1/17",
"9/1/17", "9/1/17")), .Names = c("id", "crime_type", "incident_date"
), row.names = c(NA, 6L), class = "data.frame")
> glimpse(new_incidents)
Observations: 6
Variables: 3
$ id <int> 18304380, 18304383, 18304385, 18304388, 18304390, 18304392
$ crime_type <chr> "COMMON ASSAULT", "AGGRAV ASSAULT", "INVESTIGATE", "DISORDERLY", "COMMON ASSAULT", "CO...
$ incident_date <chr> "9/1/17", "9/1/17", "9/1/17", "9/1/17", "9/1/17", "9/1/17"
我有一个我想要过滤的先前定义的日期
some_date <- as.Date("2017-09-01")
我尝试更改new_incidents $ incident_date
new_incidents$incident_date <- as.Date(new_incidents$incident_date)
> glimpse(new_incidents)
Observations: 6
Variables: 3
$ id <int> 18304380, 18304383, 18304385, 18304388, 18304390, 18304392
$ crime_type <chr> "COMMON ASSAULT", "AGGRAV ASSAULT", "INVESTIGATE", "DISORDERLY", "COMMON ASSAULT", "CO...
$ incident_date <date> 9-01-17, 9-01-17, 9-01-17, 9-01-17, 9-01-17, 9-01-17
现在,如果我尝试过滤some_date
new_incidents_smdate <- filter(new_incidents, incident_date == some_date)
返回一个空数据框。
看起来像日期格式的问题。如何根据日期格式some_date?
读入和过滤数据框答案 0 :(得分:1)
我看到你的 incident_date 列是字符类型,而 some_date 变量是 Date 类类型。这些比较将始终返回false,因此,过滤器确实会返回一个空数据帧。
您有两种选择:
as.character(as.Date(...))
new_incidents$incident_date = as.Date(new_incidents$incident_date,
format = "%M/%d/%y")
答案 1 :(得分:0)
我将更改为日期格式的行更改为:
new_incidents$incident_date <- as.Date(new_incidents$incident_date, format = "%M/%d/%y")
所以我添加了格式参数。
想知道是否有更好的方法,例如在导入时设置此格式而不是占用一个全新的行?