I currently am very new with R and am working with stock data. I am trying to set up a date and closing price dataset with 3 different stocks. I have merged all 3 stocks by date into one dataset, but now I have no clue how to get R to recognize my column "Date" as actual dates, instead of numerals. I need to plot date by price for these stocks. I have dabbled with as.Date() but I think that the necessary format for this command is 01/01/15, whereas the format I have for my data is in 1/1/15. Long story short, I cannot change the format in Excel then import it back over, so I am currently stuck with 1/1/15 format and unable to get R to recognize my data as dates. Any help would be greatly appreciated!
Sorry for wall of text.
So, the format it expects (assuming that's 1 January 2015?) is "2015-01-01" or similar. You can use base R's tools but they're more painful for you as a user than say, lubridate - a package designed just for date formatting that includes something for handling day-month-year dates:
install.packages("lubridate")
library(lubridate)
day <- "1/1/15"
as.Date(dmy(day))
[1] "2015-01-01"
Give that a whirl, see if it works for you.
Related
I am trying to replace the read.SAS7bdat function with read_sas from the haven package in a number of my programs due to speed. Simply substituting it in works perfectly and reads so much quicker. However, the only hang-up I encounter has to deal with dates and times. For some reason, I can no longer subset by a date selected in an R Shiny date input even though the underlying data looks the same and all other functions work. If anyone knows of a difference between how these two functions read dates that would be greatly appreciated.
Date zero in SAS is 1 Jan 1960, and in R (origin date) it is 1 Jan 1970. That might be the reason for your issue. Bring in dates as character from SAS, and then convert it to numeric in R.
There were two components: first I had to change the origin date to 1970 instead of 1960 as I previously was using with read.sas7bdat. I also had previously converted everything to a POSIX date, which worked fine. However, subsetting by an R shiny date input wasn't working with read_sas so I converted the posix using as.date and this resolved it. Not exactly sure why though.
Just looking for help working with some dates in R. Code for a simple data frame is below, with one column of start dates and one column of end dates. I would like to create a new column with the difference in days between each set of dates - start date and end date. Also, the dates are in different formats, so is there an easy way to convert all dates to a similar format? I've been reading about the lubridate package but haven't found anything yet on this particular situation that is easy for me to quickly learn as an R newbie. It would be great to link the answer to the dplyr pipeline as well, if possible, to calculate average number of days, etc.
Start.date<-c("05-May-15", "10-June-15", "July-12-2015")
End.date<-c("12-July-15", "2015-Aug-15", "Sept-12-2015")
Dates.df<-data.frame(Start.date,End.date)
I have a spreadsheet on which one of the columns is a date. When importing that SS to R, most of the columns have the right information, but the date column has the row number instead of the date. I'm using openxlsx. Any idea on what the problem is?
Try loading your data with readxl package. Loads very fast and keeps most data in the right format. Otherwise, you could try XLConnect slower but more versatile.
Is by any chance this happening?
as.numeric(as.Date("29.3.2016", format = "%d.%m.%Y"))
[1] 16889
If yes, then be amazed at this.
diff(as.Date(c("29.3.2016", "1.1.1970"), format = "%d.%m.%Y"))
Time difference of -16889 days
What is going on? Each date has an origin, and by default it is set to that wonderful day of January 1, 1970. If you coerce a date to numeric, the result is the difference between from the origin. See how R handles dates.
As a new and self taught R user I am struggling with converting date and time values characters into numbers to enable me to group unique combinations of data. I'm hoping someone has come across this before and knows how I might go about it.
I'd like to convert a field of DateTime data (30/11/2012 14:35) to a numeric version of the date and time (seconds from 1970 maybe??) so that I can back reference the date and time if needed.
I have search the R help and online help and only seem to be able to find POSIXct, strptime which seem to convert the other way in the examples I've seen.
I will need to apply the conversion to a large dataset so I need to set the formatting for a field not an individual value.
I have tried to modify some python code but to no avail...
Any help with this, including pointers to tools I should read about would be much appreciated.
You can do this with base R just fine, but there are some shortcuts for common date formats in the lubridate package:
library(lubridate)
d <- ymd_hms("30/11/2012 14:35")
> as.numeric(d)
[1] 1921407275
From ?POSIXct:
Class "POSIXct" represents the (signed) number of seconds since the
beginning of 1970 (in the UTC timezone) as a numeric vector.
I have a dataset with 10 columns, one of which is date in the following format
10-MAR-12 00.00.00.000000000
I would like to convert this into a data format which is read as a date and not as a string in the following format
10/03/12
I would also like there to be an additional column that says what day of the week it is
I would then like to filter out certain days or dates and to create a subset of my data.
I am a beginner to R so any help is appreciated
Take a look at ?strptime for formatting options and as.Date or as.POSIXct for the function to convert. Also, don't be surprised if your question is down voted or closed since this is a common question and answers can be found on SO or from quick google searching.
Specifically:
format(as.Date(tolower('10-MAR-12 00.00.00.000000000'), format='%d-%b-%y'), format='%d/%m/%y')
should give you the formatting you're looking for. If you want a date type though you should take off the outer format.