1

I have a question concerning the conversion of dates in R. I have a data set with a date column indicating the day of job commencement in an organization in format "dd.mm.yyyy" (e.g., 01.09.2016) (Variable jobstart). As I want to calculate the tenure in years (2018-year of job commencement), I first need to convert the dates from the format dd.mm.yyyy to yyyy.

I tried the following:

data$tenure <- as.Date(data$jobstart,format="%d.%m.%Y")
as.Date(data$tenure,format="%Y")

and

data$tenure <- as.Date(data$jobstart,format="%d.%m.%Y")
format(data$tenure,"%Y")

The first one didn't work at all and for the second I got the following output:

 [1] "2013" "2011" "2000" "2005" "2016" "1987" "2010" "1985"
 [9] "1994" "1998" "1985" "2006" "2003" "1985" "1985" "1991"
[17] "1987" "2006" "1999" "2010" "2013" "1996" "2018" "2017"
[25] "2016" "2017" "2001" "1999" "1998" "2010" "1995" "2002"
[33] "2017" "1984" "2006" "1995" "2017" "1991" "1995" "1999"
[41] "2017" "2015" "1991" "1986" "2011" "2011" "2017"

First, I was really happy but when I looked again in the rows in the dataset, it didn't convert the dates...

Can someone help me?

Thanks in advance!

user10293275
  • 47
  • 1
  • 6
  • `data$tenure = format(strptime(data$tenure,'%d.%m.%Y'),'%Y')` or `data$tenure = sub('.*\\.','',data$tenure)` – Onyambu Aug 30 '18 at 06:04
  • Have a look at the `lubridate` package, it has tons of nice function to handle data formats! – drmariod Aug 30 '18 at 06:04

2 Answers2

1

The format function converts it, but you haven't actually stored it anywhere. If you hope to overwrite the old data try

data$tenure <- format(data$tenure,"%Y")

Or you can create an entirely new column to store this so you don't lose the data.

data$tenure_year <- format(data$tenure,"%Y")

When you just use format on its own, it returns all those dates and prints them to the consoles. When you assign them to a variable, it places it there instead.

Hope that helps!

Edit: To explain why the first effort you made didn't work, it's because as.Date turns something into a date object, this specifically stores day, month and year. So if you want to just display the year, you can't store it as a date. The way you tried to use the function, you were trying to tell it to read the entire date as just a year, and that was never going to work.

LachlanO
  • 1,152
  • 8
  • 14
  • @user10293275, if this helps you, please accept the answer. It gives reputation bonuses to both you and LachlanO. – phiver Aug 30 '18 at 06:56
0

Also year from the lubridate package works:

library(lubridate)
x <- ymd("2018-08-30")

year(x)
# [1] 2018
RLave
  • 8,144
  • 3
  • 21
  • 37