Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何以特定格式导入日期_R - Fatal编程技术网

R 如何以特定格式导入日期

R 如何以特定格式导入日期,r,R,下面是一个小数据框: 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_

下面是一个小数据框:

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"
新事件一瞥(新事件)
意见:6
变量:3
18304380、18304383、18304385、18304388、18304390、18304392美元
$crime_类型“普通攻击”、“加重攻击”、“调查”、“扰乱秩序”、“普通攻击”、“共同犯罪”。。。
$事件发生日期“2017年9月1日”、“2017年9月1日”、“2017年9月1日”、“2017年9月1日”、“2017年9月1日”、“2017年9月1日”
我有一个以前定义的日期,我想根据它进行筛选

some_date <- as.Date("2017-09-01")

some_date我将更改为日期格式的行更改为:

new_incidents$incident_date <- as.Date(new_incidents$incident_date, format = "%M/%d/%y")

new_incidents$incident_date我看到的是,您列的incident_date是字符类型,而某个_date变量是日期类类型。这些比较将始终返回false,因此,筛选器确实将返回空数据帧

您有两个选择:

  • 将事件日期列作为字符使用。但是,对于此列,某些日期也必须是字符。因此
    as.character(as.date(…)
  • 将“事件日期”列用作日期。必须强制转换该列的元素:
    new\u incidents$incident\u date=as.date(new\u incidents$incident\u date,
    format=“%M/%d/%y”)

导入是什么意思?从磁盘文件导入?
new_incidents_smdate <- filter(new_incidents, incident_date == some_date)
new_incidents$incident_date <- as.Date(new_incidents$incident_date, format = "%M/%d/%y")