Video není dostupné.
Omlouváme se.
Convert to Date in R - Character Strings to Date Format
Vložit
- čas přidán 19. 08. 2024
- Most of the time when we load in CSV or Excel files, we expect that the variables will be the types we expect them to be. Number columns to be numbers, and dates to be dates.
But if we actually want to take advantage of all of the great functionality of the lubridate package that helps us work with dates in R, we need to use a different format.
While R will consider our date strings to just be a character, we can easily convert the column into a proper Date format that will behave more how we'd expect it to.
In this video, I show you a brief demonstration of the power of the lubridate package and how we can use a few handy functions to convert dates in almost any format into an actual, proper, date column.
At the end, I show you how we can put it all together in a clean chunk of code using dplyr and the piping functionality that comes with it. That will not only make our lives easier when coding it (no more pesky dollar signs to access variables), but will also make our code very readable if we need to come back to it months down the road.
Very trick and at time daunting working with dates in R. But you made it so simple and straitforward with the mdy(), ymd() and dmy() functions. Well done!
Thanks for the kind words, Osorio!
Thank you for simplifying this function !
Happy I could help!
Thanks, very helpful!
Thanks! I'm glad it was helpful
Thank you so much, i was so frustrated
Thanks for the positive feedback! If you have any other R problems you're frustrated by let me know and I may be able to make a video to help you out.
Thank you!
Glad I could help!
Thank you
You're welcome!
Thanks
You're welcome, Yaw!
Hi, thanks for this video, it's very helpful. If you don't mind me asking, what would you do in cases where the date and time are given together in a dataset? for example "Dec 04, 2021, 11:30:00AM" all in one column. I'm unsure of the best way to split them out and format them as dates/times. Thanks!
Thanks for the question. To just convert that directly into a date format, you could use the mdy_hms() function within the lubridate package.
Then, to get something like just the date without the time, you could use the date() function on that newly created variable. The same works for if you want to get the year, month, day, hour, minute, second.
Then, if you wanted to also get a column that was just the time, you could load in the hms package and use the as_hms() function on the formatted datetime object.
Here's some simple code showing a few of these things:
dates %
mutate(date_format = mdy_hms(dates)) %>%
mutate(just_date = date(date_format)) %>%
mutate(month = month(date_format)) %>%
mutate(hour = hour(date_format)) %>%
mutate(just_time = as_hms(date_format))
is there a function for month/year or is it just for d/m/y?
There are a couple of ways I'm interpreting your question, so I'll try to answer both here.
1. If you're looking to get month and year as separate variables. You could use separate() in the tidyr package to split it into two columns. Something like:
df %>% separate(date, into = c("month", "year"), sep = "/")
2. To convert month/year into a formal date object. Off the top of my head, that's not possible without adding the day in to the date. So you would need to do something like:
date
How do you retain the format of month-day-year?
For the column in your data frame? Date variables will always just show as Year-Month-Day. If you wanted to have a variable that displays it as Month-Day-Year, you could just paste it together in that way in a different column. However, you'd still use your actual date column if you wanted to run any calculations with that date variable.
@@danieldbonneau omg. Thanks . God I spent hours on this lol . So how would display my date like in a histogram using year -date? Thanks . R is fun but challenging at first .
My dataset is different. The column names are the dates and not the cells below them. It doesn't seem to work.
If your column names are dates, you want to start by reorganizing your data set. Your data set should (almost always) be in a "long" format, where each column holds one particular piece of information - such as date or sales.
So before applying the conversion from this video, you're going to want to use pivot_longer() from the tidyr package.
Let's suppose your data set has dates as the column names, and sales as the values within the first row of your data set and that this data set is called "my_data". You would run this code first to reorganize your data into two columns: "date" and "sales"
pivot_df % pivot_longer(cols = everything(), names_to = "date", values_to = "sales")
With that line, you should be left with a new data set that has two columns, date and sales. Then, you can transform the date column into an actual date data type using the steps in this video.
To see the pivot_longer code in action with a different example, I have this video: czcams.com/video/EKmB2SCm3j4/video.html
how to convert a number such as this '41738.955555555556' into date ?
You can use the as.Date() function to convert that to a date. However, you'll need to also supply an origin argument.
I've never seen it with decimals, but it's the same with decimals or the whole number.
Running as.Date(41738, origin = "1899-12-30") gives 2014-04-09. Is that the date you're looking for?
The way this works is the numeric value (41738 in this case) is the number of days from the origin supplied. In other words, 2014-04-09 is 41,738 days from 1899-12-30.
Hope that helps.
@@danieldbonneau thanks