chribonn
chribonn

Reputation: 457

R use of '\' in a string

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

Upvotes: 0

Views: 116

Answers (1)

Will Beason
Will Beason

Reputation: 3551

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"

Upvotes: 1

Related Questions