Search code examples
ras.date

For loop in r for if else statements


enter image description hereGood morning, I can't quite grasp what I am doing wrong here, could someone assist? I am trying to convert my datetime in r but some of my dates are "Jan." or "Aug." so I get null values when I try to convert it to date/time.

My solution is to separate by delimiter, grab the month, rename the month, put the date back together, and then go from there. I can't figure out the loop though and keep getting "July".

My dataframe is separated now into July 14 2022 (df$left, df$middle, df$right)

for (month in df$left){
    if (df$left == "July")
    {df$month <- "July"}
    else if (df$left == "Aug.")
    {df$month <- "August"
    if (df$left == "Sept.")
    {df$month <- "September"}
    if (df$left == "Oct.")
    {df$month <- "October"}
    if (df$left == "Nov.")
    {df$month <- "November"}
    if (df$left == "Dec.")
    {df$month <- "Dec."}
    if (df$left == "Jan.")
    {df$month <- "January"}
    if (df$left == "Feb.")
    {df$month <- "February"}
    if (df$left == "March")
    {df$month <- "March"}
    if (df$left == "April")
    {df$month <- "April"}
    if (df$left == "May")
    {df$month <- "May"}
    if (df$left == "June")
    {df$month <- "June"}
    if (df$left == "July")
    {df$month <- "July"}
    if (df$left == "Aug.")
    {df$month <- "August"}
}
left    middle  right   Workout.Date    Activity.Type
<chr>   <chr>   <chr>   <chr>   <chr>   
1   July    14, 2022    July 14, 2022   Run

Solution

  • The lubridate backage is fairly clever at working out how to interpret a date. I'm using the tidyverse simply for formatting and showing the column type easily.

    First, create some test data

    library(lubridate)
    library(tidyverse)
    
    d <- tibble(Workout.Date=c("July 14, 2022", "Jul. 14, 2022", 
                               "September 1, 2021", "Sept. 1, 2021"))
    

    Now, a one line solution:

    d %>% mutate(Workout.Date=mdy(Workout.Date))
      Workout.Date
      <date>      
    1 2022-07-14  
    2 2022-07-14  
    3 2021-09-01  
    4 2021-09-01