adding a column in dataframe an R date format - r

Question: Create a new reldate column in the movies data frame in R by converting the column release_date into R date format.
This is my code:
movies <-read.csv("C:/Users/phili/Downloads/movies500.csv")
movies
movies$reldate <- format(as.Date(movies$release_date),"%d/%m/%Y")
print(movies)
Unfortunatly the second code does not add a new column in as R date format.
If you can't answer my question directly, please use a very similar example

In the future it would be helpful to see your data or similar example data instead of a screen shot.
Anyways, looks like there are three things that need to be fixed:
You probably don't need to use the format() function. What you might have wanted is the format= argument within the as.Date() function
"%d/%m/%Y" this part tells R what format to expect the dates should be in, your dates are year month, day, so the order is wrong
similarly your dates are separated by dashes not slashes
So it should look like this: as.Date("2018-09-12",format="%Y-%m-%d")
So in your example try this: as.Date(movies$release_date,format="%Y-%m-%d")
Or because one of the default for as.Date() is "%Y-%m-%d" you could probably just do as.Date(movies$release_date)

Related

Gather turns my dates into unrecognisable format

I am trying to gather a couple columns of dates so that its easier for it to be choices in shiny. However, when I gather dates, it turns into for example, 2020/12/14 to 128284 format. I have tried as.Date, as.character, I have tried lubridating but it doesn't work. (I have been gathering in a separate script besides shiny). Please see my code when gathering.
Here is my data
before gather
df<-df%>%gather(key="date.type", value="dates",
date.1, date.2, date.3, date.4)
This turns it to something like this;
after gather
This becomes a problem when I am trying to find difference between two dates in Shiny(I have been using difftime).
The error I get in shiny is:
x character string is not in a standard unambiguous format
I am also thinking of not gathering at all, but allowing the user to choose the from date column and to date column in the UI, but I am not sure how to then find the difference in days between the from and to dates in the server.
mutate(theduration=difftime(input$to,input$from,units="days")
This doesn't work.
OK, so I had this problem when using gather to make a dataset. When you get those 5 digit time character blocks, try this:
mutate(time=as.Date(as.numeric(time),origin="1899-12-30"))
Apparently, that that 5 digit number is days since the origin date. It's a MS thing. Good Luck!

Looking for advice on creating Tidy data from the start

I have a data set that will be growing. It is categorical observations (i.e., 1=yes, 2=no) by date and hour. Is the following an acceptable method of formatting for import to R or is there a better way?
I would use a template like this:
Using one column for the date makes it much easier to read/import into R. Also, the YYYY-MM-DD is the default format in R for date columns. Trying to write date and hour together in one column could be done but seems like it could be tedious and not as easy to see what is going on in the data. As was mentioned in the comments above, each observation should be on a separate row. Once you save the data as a csv, it will be easily imported into R.
Good luck.

Trouble getting R to recognize Date data as Dates

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.

Dates on X Axis

I have looked everywhere ( Example ) and a few other posts, but I just cannot understand how to setup the date on the X axis. Can someone help me? This is what I have so far:
Data.CSV:
21-Oct-14,
22-Oct-14,
23-Oct-14,....etc
I have tried this: axis(1,at=NVDA_Data$Date) and it doesn't show up and the console says its NULL
I just want to change where it says 0-250 with the dates
You can improve your question by letting us know you are using base R plot (which it appears from your screen shot to be true).
One easy solution is to convert your date-looking character or factor variable into a Date variable. Plot will generally make reasonable axis marks from a Date variable. Use summary() on your data frame to determine if the Date variable is a factor or character. If it is a character, then do something like this:
data.frame$Date2 <- as.Date(data.frame$Date, "%d-%b-%y")
If your Date column is read in as a factor, use colClasses to read it in as character, then use the snippet above.
The link #user20650 provided gives some good tips for labeling Date variables.

How do I convert a string into date and day in R

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.

Resources