如何从csv文件将R中的列更改为日期

时间:2018-09-09 03:09:00

标签: r

我正在尝试将某些列更改为日期,但是出现错误

错误

> data %>%
+    group_by(data$Metric) %>%
+    mutate(data$ReportDate=as.Date(data$ReportDate, format = "%d.%m.%Y"))
Error: unexpected '=' in:
"   group_by(data$Metric) %>%
   mutate(data$ReportDate="

脚本

data = read.table("/home/mylaptop/Downloads/ipynb_checkpoints/hello.csv", header=TRUE)

> lapply(data, class)
$ReportDate
[1] "factor"

$Value
[1] "integer"

$Metric
[1] "factor"

$dow
[1] "factor"

$week
[1] "integer"

$doy
[1] "integer"

$weekStart
[1] "factor"

$Rescaled
[1] "numeric"

图片 enter image description here

转换为日期

data %>%
   group_by(data$Metric) %>%
   mutate(data$ReportDate=as.Date(data$ReportDate, format = "%d.%m.%Y"))

日期列的预期输出

> lapply(data, class)
$ReportDate
[1] "Date"

$Metric
[1] "factor"

$Value
[1] "numeric"

$dow
[1] "ordered" "factor" 

$week
[1] "numeric"

$weeks
[1] "factor"

$weekStart
[1] "Date"

我正在关注this tutorial,但我想从csv文件中获取数据

1 个答案:

答案 0 :(得分:2)

使用lubridate软件包的替代方法是:

"(&(objectCategory=person)(objectClass=user)(cn=ABC DEF))"

或更简单地说:

library(lubridate)
data %>%
  group_by(Metric) %>%
  mutate(ReportDate = ymd(ReportDate)