Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finding yesterday's date using R

Tags:

date

format

r

In one of my R scripts I need to find the date of yesterday, I can do that easily on my mac with this command.

yesterday <- format(Sys.Date()-1,"%m/%d/%Y")
yesterday
[1] "03/17/2017"

So essentially the Sys.Date()-1 gives me yesterday's date. However, when I run this command on my ubuntu 16.04 machine(AWS instance) it doesn't work. It returns today's date instead? Has anyone else experienced this? How could I get yesterday's date without using the Sys.Date()-1 command?

UPDATE: Amazon AWS says the server location is in N. California, however upon further inspection it seems that the server's timezone is in fact 7 hours ahead? I used this tool, IP Location Finder to try to geo-locate the IP address, and the IP was found in N.Califorina? Confusing but for now I'm just adding 7 hours to my date variable and it's a short term fix, but I would like to understand what's causing this timezone issue.

like image 744
Developing Avatar asked Mar 19 '17 01:03

Developing


People also ask

How do I get the last day of the previous month in R?

The first date of the month in R If you have the first date of the month, then it is easy to calculate the last day of the previous month by simple subtraction, but it will work with the class of Date. The last day of the previous month can be calculated with the rollback function from the lubridate package.

How do I view dates in R?

In R programming, if you use Sys. Date() function, it will give you the system date. You don't need to add an argument inside the parentheses to this function.

What is POSIXct in R?

POSIXct stores both a date and time with an associated time zone. The default time zone selected, is the time zone that your computer is set to which is most often your local time zone. POSIXct stores date and time in seconds with the number of seconds beginning at 1 January 1970.

Is there a date data type in R?

In addition to the time data types R also has a date data type. The difference is that the date data type keeps track of numbers of days rather than seconds. You can cast a string into a date type using the as. Date function.


1 Answers

Sys.Date returns an object of class "Date" while Sys.time returns an object of class "POSIXct" (of DateTimeClasses).

Sys.time will be much easier to work with for manipulating exact times, since "POSIXct" is the number of seconds post epoch as opposed to a Date object. See: https://www.epochconverter.com/ to get a better understanding of epoch time.

Regardless, either method should work and it might just be your Ubuntu system isn't on the correct time.

like image 165
Z. Bagley Avatar answered Sep 28 '22 01:09

Z. Bagley