0

I am experiment with R and came across an issue I don't fully understand.

dates = c("03-19-76", "04/19/76", as.character("04\19\76"), "05.19.76", "060766")
dates

[1] "03-19-76" "04/19/76" "04\0019>" "05.19.76" "060766"

Why should the third date be interpreted and what sort of interpretation is taking place. I also got this output when I left out the as.character function.

Thanks

2
  • 2
    Take a look here Commented Aug 25, 2014 at 10:36
  • 2
    If I understood correctly, backslash characters need to be escaped dates = c("03-19-76", "04/19/76", "05.19.76", "060766", "04\\07\\13") Commented Aug 25, 2014 at 11:26

1 Answer 1

1

Echoing the comments, make sure to escape backslashes in strings.

dates = c("03-19-76", "04/19/76", "04\\19\\76", "05.19.76", "060766")

> dates
[1] "03-19-76"   "04/19/76"   "04\\19\\76" "05.19.76"   "060766"

Now that you've got the dates stored, there's actually a lot of built in functions you can use with dates. Dates even have their own object types! To do so use as.Date. Since you're using nonstandard date formats, you have to tell R how you've formatted them.

> as.Date(dates[1], "%m-%d-%y")
[1] "1976-03-19"

> as.Date(dates[2], "%m/%d/%y")
[1] "1976-04-19"

> as.Date("20\\10\\1999", "%d\\%m\\%Y")
[1] "1999-10-20"

a <- as.Date(dates[1], "%m-%d-%y")
b <- as.Date(dates[2], "%m/%d/%y")

> b - a
Time difference of 31 days

d <- as.numeric(b-a)

> d
[1] 31

> a + d^2
[1] "1978-11-05"

Note that since you're using 2-digit years, you use %y. If you used 4-digit years, you'd use %Y. If you forget, you'll get oddities like this:

> as.Date("03/14/2001", "%m/%d/%y")
[1] "2020-03-14"

> as.Date("03/14/10", "%m/%d/%Y")
[1] "0010-03-14"
Sign up to request clarification or add additional context in comments.

2 Comments

I would like to add date = "20\\10\\1999" as.Date(date, format="%d\\%m\\%Y")
Added! Anything else I can do to answer your question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.