 I believe you can answer your own data analysis questions. Do you? If you do, stick around for this week's episode of Code Club. My name is Pat Schloss, and my goal is to help you to grow in confidence and to gain the skills you need to answer your own data analysis questions. Running our farm is a lot of work. Every day there's all these little activities that we have to do to make sure that our animals are well fed and cared for to make sure they're healthy. We've got to make sure that in our gardens, things are growing and we've got to get rid of the weeds. We've got a mow. We've got a lot of land that we've got to take care of, the house we have, all the kids we have. It's just a lot of work day in, day out. And sometimes it's easy to lose track of kind of the long view of where we're going over the course of a year or several years, right? And so on our farm, one of the things that we do is that we breed to produce our own animals that we later market for meat. Some people will buy in their own piglets or their own lambs, feed them, and then have them processed for meat. We use reproduction to grow our own herds and flocks so that we can later have these animals that we raise for meat, like I said. Also my kids like to raise animals for our local 4-H fair, not this summer because of the pandemic, but in other summers they raise them and show them at our local 4-H fair. And so planning ahead to make sure everyone has the animals they need is a serious task, right? We don't want to screw up our dates to make sure, we want to make sure everything's born when we need them to be born so that they're ready at the end of July when we typically have our fair. I'm also interested in growing my flock of sheep. My son is interested in growing his herd of cattle. And so we need to make sure that these animals are born or having babies at the right time of year. A few years ago we had a couple of calves born in the beginning of January in a bit of a snowstorm. Everything worked out I think, but it was not ideal. If we'd have consulted a calendar a little bit more closely, we could avoid some of those problems. So that is exactly what we're going to talk about on today's Code Club. How can we use functions within R using a package called LubraDate, kind of like LubraKate but with a D instead of the C, LubraDate, to help us to work with calendar functions, dates, times, things like that for building into our data analysis. So I could ask the question, you know, if an animal was bred today, when would I expect them to have their offspring if I know their gestation period, right? So one of these gestation periods that is fairly popular among people that farm is the gestation time for a pig. And so a sow will give birth to maybe 12 or so, eight to 12 piglets. But her gestation period is three months, three weeks, three days, okay? And so that's easy to remember. It's kind of catchy. Three months, three weeks, three days. But I don't know how many days that is, right? And if you think about it, some months have different numbers of days, right? Some months have 30 days, some have 31. Well, February has 28, but sometimes it has 29. Actually, this year it had 29 days because it was a leap year. And so depending on where you are in the calendar, you can get a different number of days for that three months, three weeks, three days, right? So we're going to work on that in R. We'll also talk about that problem of breeding cattle to make sure that they're born after March 1st of the year. So if they're born after March 1st, well, when do they need to be bred? And also we like to have our lambs born three times a year so that each U has three sets of lambs every two years. So we have different sets of U's in our flock that give birth at different times of the year. And so how do we set that up? So these are all things that we'll talk about in today's Code Club. So join me now as we go over to the Riffamonus website, riffamonus.org, forward slash code club. And here we will see the title for today's Code Club. Let's do it. Stealing song title from Elefant's Gerald, I believe. And the more modern version is something I'm a fan of by Joan Jett, which was in the 90s movie Tank Girl. One of the tools that I've found to be really useful are what's called gestation tables. And here is an example of one for pigs. And so if I had a sow that was bred today, so today being May 28th, I would look at this line that says, bred May. Scroll over, scan over to the 28. And below that, I see 19. And look to the right, that says September 19th, right? So if a sow was bred today, she would be farrowing, as we say, giving birth to piglets on September 19th. Now, we actually had a sow that was bred on April 7th. And so again, I would look on this line, bred April, scroll over to the 7, down to the 30, and see that she's going to farrow July 30th at the end of July. So this is kind of a handy table that we have. It works really well for a handful of animals or for looking up a handful of dates. But sometimes we want to do things that are a little bit more complicated, a little bit more sophisticated, and certainly as you adapt to your own interests or other applications where you want to use dates and differences in dates, difference in time between dates, there might not be a table like this. So what we want to do is kind of make something like this but for our own uses and our for our own types of questions. We'll get going in our studio now. And the first things that we have to do when we open up a fresh instance of our studio is to run these LuberDate functions is to use library LuberDate. And we're also going to use features from the tidyverse package. So LuberDate is installed when you install tidyverse, but when you library tidyverse, it doesn't also bring in the functions from LuberDate. So we need to run both of those commands when we first get into R. So one of the first things to talk about is how do we represent a date? So one of the memes that people typically in Europe like to push back at the United States is a global map. And all the countries are color coded according to whether or not they use the imperial system of measurement, so things like pounds and feet, right? So the only country that's lit up is the United States. So we're a little bit behind the times here in the US. Anyway, if we were to make a map like that of people that adhere to the correct standard, the metric system, so to speak, for dates, nothing would be highlighted. And so that is what's called ISO 8601. This is actually a standard on the correct right way to write a date. And I can guarantee you that you're probably not writing a date, and if you're outside of science, you're definitely not writing dates the correct way. So let me open up a script file here. And so again, if today is May 28th, 2020, we might write that in different ways. You might say, wow, Pat, that's a weird way of writing it. Over in Europe, perhaps people write it like this, 28 May, 2020, or you could write it numerically, five, 2020, or you could write five, 2020, right? Or others might write it as 25, 2020. You kind of get the drift. And if we were in January, we might have different abbreviations, right? It might be like Jan 1, 2020, or January 1, 2020. And so there's a lot of ambiguity in how we write dates, that there's a lot of, maybe not ambiguity, but inconsistency in how we express the writing of our dates. These types of formats are relatively easy for a computer to figure out. There are dates where it's not so easy to figure out, right? So what about February 5th, 2020? How would we write that numerically? Well, the convention that I grew up using would be 25, 2020. But again, a more European system might write five to 2020. And I'd look at that and say, oh, May 2nd, right? And so for those dates that are earlier in the month, there's ambiguity as to which number is the month and which is the day. So unless you tell the computer which format you're using, it's confusing. So Lubridate has a series of functions for converting examples like these into actual dates, converting it to the ISO standard. And I've talked a lot about this ISO standard, but I haven't actually described it yet. And so, like I said, the ISO standard is quite a bit different from these formats of dates where we might say 2020, 05-28. And the syntax here is I have four digit year, a two digit month, so like May would be 05, October would be 10, and then a two digit day. So if it's the fifth of the month, it'd be 05. 28th of the month, like today would be 28, okay? So year, year, year, year, hyphen, month, month, hyphen, day, day, okay? And the nice thing about this is that all your dates will be the same number of characters. If you sort the dates, often numerically, they'll be in chronological order, right? With these other date formats, if we sort them, they won't necessarily be in chronological order, right? Because we'd like to sort on the year and then the month and then the day. And so if you think about it in that context, it makes a lot of sense why this ISO standard is the way it is. There's also a great XKCD comic about this ISO standard. So anyway, like I said, lubricate can take in dates in all these different functions, different formats using different functions and different syntax you give to those functions. I'm not gonna talk about those today. I'm gonna talk about the date function so how we can convert this ISO standard text date into a date that R knows as a date. Again, look through the documentation for lubricate to see how to convert these other formats into a date format that R can understand. And so for today, we would say like today would be date 2020, 05, 28. And if we look at today, we see it comes up looking like a string. But if I do today and pipe that into the glimpse function, we see that it's date and that its format is 2020 hyphen 05 hyphen 28. There's also a function that we could use which is now. And so the now function gives you the date and the time that it is right now. So I'm actually recording this on the 26th. And so you can see that it comes up as May 26, 2020 at 7.56 am and 44 seconds. And I'm an Eastern daylight time. So now is a really useful function for getting that as well. All right, so let's start talking about how we can use arithmetic to modify our dates. So about a month ago on April 25th, I put my two rams in with a flock of about 25, 27 years. My goal is to have lambs born in the fall that I can then feed over the fall and into the winter so that there's lamb for people that want lamb for say Christmas. And so if the, if I say breeding date, if I say breeding date, I'll say breeding start, we'll say date 2020, 04, 25. And then I could say, well, lambing start would be breeding start plus the gestation period for a lamb, which is 147 days. I don't wanna add 147 because the base functions of lubricate assume the unit of time as a second. So instead I'm gonna give it days. So days 147. And if I run these two lines, I see that my lambing start will start on September 19th of 2020. So like I said, the rams are still out there. I'm gonna remove them here this weekend on May 30th. And so that will be breeding end plus days 147. Well, I'm gonna find both variables. And so lambing end will be October 24th. So I'll go from September 19th to October 24th. I use, I've relation cycles about 17 or 18 days. And so in this period of time that I've had the rams out there, there'll have been two opportunities for those use to become pregnant. So if they all get, got bred on this first cycle, then things probably won't go out till middle of October like that, okay? So again, seeing that we can use this days function to add on 147 days. There are other functions like days, including years, months, weeks, hours, minutes, seconds, nanoseconds, picoseconds, milliseconds. We're not gonna deal with the clock time issues we're gonna deal with calendar time. And so as I mentioned, there's this heuristic of three months, three weeks, three days. And if I now think about my pigs that we saw our sow being bred on April 7th. And so if we say breed date, 2020, 04, 07. So you'll get good practice with this tutorial of learning the ISO standard. And so if we do breed date plus, much like we did with days 147, I can then do months, three, weeks, three, days three. And I see then that I would expect them to be born on July 30th. I think with that farrowing table that I had up, that it was right about the same time. I'm already forgetting what time I think it's, I think it was maybe July 29th, I said. And so again, this is a heuristic and because the number of days in a month varies across the calendar, we might be plus or minus a couple of days. The actual period, gestation period is 114 days. So we could say breed date plus days 147. Plus days 147. And that, sorry, that's the number for sheep. 114 is the gestation period for pigs. And that gets us to July 30th, right? Which is I think what we had actually by that table. And so we see that the heuristic adds an extra day for this period of time. In other periods of time, it's gonna be two days longer. Sometimes it's gonna be right on. And some months it might actually be a little bit shorter than the actual gestation period. But in a pinch, if you don't have a table to look up that three months, three weeks, three days, it's sure memorable and easy to rattle off. So I already told you that the gestation time for pig is actually 114 days. But how many days is three months, three weeks, three days? Well, because the number of months varies depending on where you are in the calendar, we need to give a set date that we then add our months, weeks, and days to to figure out how long that period is. And this will allow us then to do some arithmetic with more arithmetic with these dates. And so I can do breed date, plus months, three weeks, three days, three. And again, that gets us to July 31st. But I could also then subtract out breed date. And it tells me that the time difference is 115 days. That's pretty nice. So I'm gonna make this a little bit cleaner by copying down these two lines. And so the breed date is that, the due date is this, right? Breed date, due date. And if I look at my due date, again, July 31st, and I could do due date minus breed date, 115 days. So the subtraction works well. An alternative that maybe gives you a little bit more flexibility is diff time. So we do diff time, due date, comma, breed date. And we see a time difference of 115 days. We could also then say units equals sex for a number of seconds, or we could do hours, or we could do weeks, right? But the default is days, 115 days. And so diff time gives you a little bit more power than straight up arithmetic does, right? Straight up, if we did due date minus breed date, we get the same output for the number of days, but say I'd wanted to convert that to weeks, well, then I need to use that diff time. And so I'm gonna be using diff time for most of the rest of the workshop, most of the rest of this tutorial. Okay, so again, we could change this date to look at, say, like February 7th, say. And we see there that the difference is 114 days. And we could also look at, let's look at like April, 115 days. We'll go ahead and remove that line. If we looked June, 116 days. If we look in August, oh, we got 116 days. And if we look in October, 116 days. So we get between 114 and 116 days. The actual gestation time of 114 days. Again, it's close, right? It's probably close enough for the applications I'm interested in at least, because I'm always gonna be looking for the salad to be in labor a couple of days before the due date anyway. All right, so again, we've seen how we can use these months, days, weeks, years, those types of functions to add time onto a calendar date. Let's think about a different application, which would be to back date a breeding time. So say my due date is March 1st. So weather in Michigan is really unpredictable here in May. As I've already talked about, we had a 25 degree weather. Yesterday it was 85 degrees. Today is supposed to be 88 degrees. So hot, but such variation, right? So I feel like if we can get past March 1st, then we'll be in good shape for our calves to be born. So if I want things born after March 1st, then the question is, well, when should they be bred after, right? I don't wanna breed before a certain date. I need to figure out what that date is. Well, very much like what we've already seen, we can use these date functions. We can use days, the days function, to subtract the number of days from the due date. And so a cow's gestation period is 283 days. And so if I subtract 283 days from March 1st of 21, actually, because it's gonna be next March, then I see that if she's bred after May 22nd, then the calves will be born after March 1st, okay? So again, we can add days on to a date. We can also remove days, weeks, months, years from a date, okay? Hopefully you feel more comfortable or you've perhaps learned something about how to work with dates within LuberDate. There's a lot of other functions in the LuberDate package that we'll probably get into in future episodes of Code Club. For now, what I'd like you to do is go ahead and pause the video and turn to the exercises that are on the Code Club website, the link to the Code Club website for this particular episode is in the notes down below. So go ahead and pause this, go if you haven't already to the exercises and there are four exercises there that will help you work through various activities using these various functions. Hopefully you found those exercises useful for growing infamiliarity in your comfort and using these various functions from LuberDate. What I'd like to do now is go through these four exercises with you to show you how I would do them. Of course you might come up with a different way but as long as we get the same answers, we're good to go. So the first question was how many days were the rams in with the use? I guess they're still in there but how long of a period have I given them? And so what we described was the breeding start. I have this above but I'll rewrite it here. 2020, 04, 25, breeding end, date, 2020, 05, 30. And then I'm gonna do diff time, breeding end, comma, breeding start. And the default units here is gonna be days. If I run these three lines, I see that the difference is 35 days. Alternatively, we could have done breeding end minus breeding start, 35 days, same result. For the second exercise, the question is we want our pigs born after January 10th so that my kids can have pigs born to show at a local 4-H fair at the end of July. So when should we expose the sows to the boar to make sure that we have piglets born around January 10th or after January 10th? And so we could say due date is gonna be date and then we're gonna do 2021 because I'm planning ahead for next year, 0, 1, 10. And I wanna do due date minus days 114, right? And so there we see September 18th. So we need to expose sows that are not bred to the boar just before perhaps a right around September 18th so that they're bred to give my kids piglets in early January that they can then raise for 4-H fair. All right, after the use that have been exposed have their lambs, they will nurse their lambs for nine weeks. That's a period that I give them. Most people wean at about eight weeks old. I find that giving them nine weeks maybe gives the use that were a little bit slow to have their lambs kind of later in the period, an extra week to spend time with their lambs and to give them the nutrition they need but generally milk production has really kind of fallen off by about I think like five or six weeks. Anyway, I give them a recovery period to gain some weight before breeding them again. So again, I'm trying to breed the use. One U will be bred three times in two years. So that way then she would lamb have lambs every eight months. So if I want her to lamb every eight months, her gestation period is 147 days and she's nursing her lambs for nine weeks, how long is this recovery period that they have to kind of regain the weight that they lost from nursing their lambs? All right, so we could say do date or let's say breeding date, say breeding start, date 2020, 04, 25. Lambing start will be breeding start plus days 147 and then weaning will be lambing start plus weeks nine. Breeding start not found, misspelled it. Okay, so my weaning date will be November 21st, right? So if I want to market these lambs for Christmas, they will then have another month to grow before they're marketed for as Christmas lambs. The Greek Orthodox religion likes to have small lambs for Christmas. Anyway, so now we're on to thinking about the next pregnancy for these use. So my lambing start two will be the lambing start plus eight months. So months eight, so we're gonna start lambing again on May 19th, okay? So when were they bred? Well, breeding start two is gonna be lambing start minus days 147, right? So we're figuring out because we know when the first group of lambs is gonna be born, eight months later they're gonna be born, another batch is gonna be born. So let me then do back date 147 days to figure out when they're gonna be bred, right? So when do I need to put rams in to breed those use so that I have lambs next to me from this group? And so that breeding start two is gonna be April 25th, right? So again, that's not quite right. Is that right? Ah, that's not right. So I need to do lambing start two minus 147 days. And so then I can see that they need to be exposed to the RAM on December 23rd, okay? It would probably be better if I had better names for my variables here. So what is the difference then between the breeding start two and the weaning date? That's the question we have here. So we could say diff time, breeding start two, comma weaning, and we see the difference of 32 days. So the use that are being bred right now will lamb in September. They'll have their lambs at their side for nine weeks and then they'll have just over a month without their lambs to recover their body conditioning and then be exposed to the rams on, we said December 23rd, and then they will lamb around May 18th or 19th, 19th of next year, okay? So again, this is really useful as a tool for planning ahead my calendar and I can use this to plan breeding. I can also use it to think about how I'm going to feed these use. The other thing I can do is that this is all one chunk of code, that's about six lines of code here, that if I'm interested in these five different dates, I could also add weaning two to be lambing start two plus weeks nine, right? That I'm interested in these six dates that by changing this initial date and rerunning those lines of code, I can get updated dates for everything. So sure, the diff time between the breeding start and weaning that recovery period will probably be about the same every time, but the individual dates will move around the calendar as I move this initial breeding start date. Okay, and this is the type of operation that is just not easy with those gestation tables. That three months, three weeks, three days heuristics is again really catchy. I really like that, I think that's kind of cool. The 283 days for cows and 147 for sheep is maybe a little bit harder to remember and not so easy to manipulate if you're thinking about kind of the calendar in your head. So can we come up with other heuristics for cows and for sheep? And so we're gonna say that the breeding day is today's date and the due date will say, let's say due date heuristic, we'll say breeding day plus let's say seven months, seven weeks, seven days, weeks, not months. And then due date gestation, we'll say breeding day plus days 283. And then we can do diff time, due date gestation, due date heuristic. And we see there's a difference here of 13 days that the actual gestation was 13 days longer than the heuristic. So I need to change this a little bit to get it to be what I want. So let's try eight months, eight weeks, we can then rerun all this, or 18 days along the other way. So that's not good. Let's try nine months. So what does just nine months get us? Seven days. So let's do nine months and nine days. Minus two days, okay? So what we see then is that we're within two days with a heuristic of nine months, nine days as the gestation period for a cow, right? So if on the 28th of May, I'd breed the cow, I'd go ahead nine months, and that gets me to February 28th, I then add nine days, and that gets me to about March 6th, March 7th, okay? That works pretty well on my head. I can't take May 28th in my head 283 days, so that's pretty slick. So this will be for cows. Let's do sheep now. And so I'm gonna copy this code because it's largely gonna be the same except for the numbers that go in there. And so instead of 283, we can use 147, and I'm gonna change my months and my days. Let's do five months and see what we get. With this. And so we see it's about six days off that the heuristic is six days longer than the actual. So let's do four months and four weeks. So within four days, and I think that's probably about as good as we're gonna get. Well, if we do four months, three weeks, two days, that gets you pretty close. Four months, three weeks, two days. I don't know if I could remember that so easily. But again, if I have lambs that go in on April, or rams that go in with my use on April 25th, I add four months, that gets me to August 25th. And then I add three weeks. That's where it gets kind of hard. So April, August 25th, September 1st, September 15th, September 17th, right? So you can kind of do it in your head a lot, definitely a lot easier than adding 147 days. I'd probably be inclined to say, well, I'll add five months and know that that's a little bit longer than it really is, right? So if I add five months to April 25th, that gets me September 25th, but I need to maybe pull back five days. So what would that look like actually? So if we do five months and then subtract five days, it gets you within a day, right? So add five months and take off five days. So April 25th, add five months, gets us to September 25th, pull back five days, September 20th, pretty good. Great, so again, I hope that this has been useful getting you to think about different ways that you can use functions from the Luberdate package to work with calendars. And while your interests and your applications and your questions are gonna be totally different than what I come up with, sorry, hopefully this, you can think about how you would generalize these approaches and these types of questions for your own research question. Thanks again for joining me for this week's episode of Code Club. Be sure that you spend time engaging in the exercises to strengthen your new skills using the functions from the Luberdate package. Even better than going through the exercises would be to take this material and apply it to questions about the world around you that are relevant to your interests. I'd love to see what you did. Please leave a comment below telling me what you did or tweet at me at patch loss. If you run into a problem, that's perfectly fine. That tells me and you what more we need to learn. So if you run into a problem, leave that in the comments as well and I'd be happy to come back at a future Code Club to help get over those issues. Please be sure to tell your friends about these Code Club episodes. You can help others to learn more about them by liking this video, subscribing to the Riffamonus channel and clicking on the bell for notifications so that you know when the next episode of Code Club is released. These Code Clubs come out every Thursday afternoon. So keep practicing and we'll see you with the next Code Club episode.