-2

I am trying to change some columns to date and I have an error

error

> 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="

script

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"

image enter image description here

convert to date

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

expected output for the date column

> 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"

I was following this tutorial but I wanted to take the data from a csv file

Papouche Guinslyzinho
  • 5,277
  • 14
  • 58
  • 101
  • 1
    `data %>% group_by(Metric) %>% mutate(ReportDate = as.Date(ReportDate, format = "%Y-%m-%d"))` – Tung Sep 09 '18 at 03:44
  • 1
    No need to use `data$...` inside the pipe `%>%` – Tung Sep 09 '18 at 03:44
  • 2
    Next time please share sample of your data using `dput()` (not `str` or `head` or picture/screenshot) so others can help. See more here https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?rq=1 – Tung Sep 09 '18 at 03:45

1 Answers1

2

An alternative using the lubridate package would be:

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

Or more simply:

library(lubridate)
data$ReportDate <- ymd(data$ReportDate)
Jay Achar
  • 1,156
  • 9
  • 16