1

Guys I've been trying to change dates given in character form to a date , but I keep getting the wrong answers or an error. This is the structure of my data: Classes ‘data.table’ and 'data.frame': 2880 obs. of 9 variables:

$ Date : chr "1/2/2007" "1/2/2007" "1/2/2007" "1/2/2007" ...
$ Time : chr "00:00:00" "00:01:00" "00:02:00" "00:03:00" ...
$ Global_active_power : num 0.326 0.326 0.324 0.324 0.322 0.32 0.32 0.32 0.32 0.236 ...

Below is what I've tried so far

Trial$Date <- as.Date(Trial$Date, "%d/%m/Y") #this gives me NAs
Trial$Date <- as.Date.character(Trial$Date, "%d/%m/Y") #this gives me an error(Error in format.default(x, ...) : invalid 'trim' argument)

please advice. Thanks

Jaap
  • 81,064
  • 34
  • 182
  • 193

1 Answers1

1

I may be misunderstanding your question, but it seems to be within the capacity of the as.Date function.

test <- c("1/2/2007", "1/2/2007", "1/2/2007")
as.Date(test, "%m/%m/%Y")
[1] "2007-02-01" "2007-02-01" "2007-02-01"

It seems to work fine. The syntax usually requires a % sign prior to the letter. http://www.statmethods.net/input/dates.html is a good intro read.

Sorry if I'm not hitting your question correctly.

asshah4
  • 164
  • 10
  • Well, I don't think you missed the Question yu just haven't provided the solution. my code is shown in the Qn, above, its very similar to yours but I need to know why am not getting results. I do know that the code is right but am sure there has to be something am doing wrong other than the code. and the error msg is also attached in the qn, thanks for taking the time to look it it tho – Claire Rukundo Sep 28 '17 at 17:32