Search code examples
rlubridate

Year function in Lubridate is not only taking the year as planned


I have been trying to use the year function in lubridate to extract only the year from a date, but it is still keeping the entire date. Can anyone help me fix this issue please?

        data %>% 
        select(ID, Author, Date, Location, Treatment) %>%     
        distinct() %>%
        drop_na() %>%   
        mutate(year = lubridate::year(Date)) %>% 
        unite('Study', Author, Date, Location, sep = " ", remove = T, na.rm = F) %>% 
        flextable() %>% 
        autofit()
  
           }

Solution

  • Your problem is that class(data$Dates) is a character column. It needs to be of class "Date" for year to understand which part is the year.

    The default formats that as.Date check when passed a string are: c("%Y-%m-%d", "%Y/%m/%d"), which match the format your character column is in (e.g. YYYY-numeric month-day).

    library(lubridate)
    library(dplyr)
    
    data %>% 
      mutate(year = year(as.Date(Dates)))
    

    Output

           Dates year
    1 1999-01-01 1999
    2 1999-01-01 1999
    3 1999-01-01 1999
    4 1999-01-01 1999
    5 1999-01-01 1999
    6 1999-01-01 1999
    

    Data

    data <- structure(list(Dates = c("1999-01-01", "1999-01-01", "1999-01-01", 
    "1999-01-01", "1999-01-01", "1999-01-01")), class = "data.frame", row.names = c(NA, 
    -6L))