I have a data frame like below
Un_ID Type Child ID Name DOB Hire_date Tenure
1 AMF 23 ASD,ghu 31-12-2016 14-01-1996 2004
2 GHK 45 dgu,khl 2/6/2010 25-05-1989 1993
3 FGT 65 hhm,mjh 11/10/2016 19-12-1990 1998
4 MHU 76 aaa,scg 11/7/2016 19-10-1993 1998
i am calculating difference between Hire date to today but i am getting answer in years (2004) and i want like 15 Years ,20 Years
trying like below
df$Tenure <- round(as.numeric(difftime(Sys.Date(),df$Hire_date,units = 'weeks')/52.25),0)
also i want to mutate a new column in data frame as T/F in 1,0 to check if Check if hire_date is > of DOB + 20 but getting error
Error in df$DOB + 17 : non-numeric argument to binary operator
df$Hire_check <- df$Hire_date>df$DOB+17
Date calculations are best done with the appropriate datetime object. The lubridate
package provides facilities.
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
a_date <- now()
a_date
#> [1] "2020-09-11 00:31:43 PDT"
the_year <- year(a_date)
the_year
#> [1] 2020
birthday <- ymd("1996-01-01")
the_year - year(birthday)
#> [1] 24
the_year - year(birthday) > 20
#> [1] TRUE
years <- paste(the_year - year(birthday),"Years")
years
#> [1] "24 Years"
Created on 2020-09-11 by the reprex package (v0.3.0)
system
Closed
3
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.