0

Report.csvHere's the code:

data = read.csv('Report.csv', stringsAsFactors = FALSE, strip.white=TRUE, as.is = TRUE)
data = data.frame(Date = "2009-04-18", data)
data$Date = as.Date(data$Date, "%Y/%m/%d")

It keeps giving me the charToDate(x) error

(character string is not in a standard unambiguous format).

I have 0 idea what is wrong. My ultimate goal is to create a date column in this data frame.

sammuh
  • 41
  • 3
  • 12
  • How about using zoo package to read csv files? `data <- read.zoo("Report.csv", sep = ',', tz = '', header = TRUE, format = '%Y/%m/%d')` – Tung Jun 16 '16 at 20:52
  • The file is VERY messy. There is no prior date column, I'm extracting it from other columns. That's why I'm trying to create a data column after reading it. – sammuh Jun 16 '16 at 20:54
  • 1
    try changing to: `data$Date = as.Date(data$Date, "%Y-%m-%d")` – Mike H. Jun 16 '16 at 20:55
  • IMPORTANT EDIT: when I run the code manually, block by block, it works as intended. But I only receive this error message when I knit HTML. Any ideas as to why? – sammuh Jun 16 '16 at 20:57
  • 1
    @sammuh: can you create a minimum [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Tung Jun 16 '16 at 20:58

1 Answers1

1

Your date variable is formatted as YYYY-MM-DD, not YYYY/MM/DD. You are trying to tell R that your separator for your date variable is / instead of - (which is what it actually is). That said, this should do the trick:

data$Date = as.Date(data$Date, "%Y-%m-%d")

Mike H.
  • 13,960
  • 2
  • 29
  • 39
  • We could be of more use if you produced a reproducible example, we have no idea what `Report.csv` looks like. – Mike H. Jun 17 '16 at 12:50
  • I attached a picture of what the csv looks like when I import it. I'm simply adding a date column to this data frame – sammuh Jun 17 '16 at 12:58