0

I need to calculate the maturity of a bone using R and I've been looking for a function similar to DATEVALUE in Excel, but I didn't find something similar. I know that DATEVALUE use the amount of days between 01/01/1900 until the date, so maybe someone knows how to do this in R.

For example, in Excel DATEVALUE(01-01-1900)=1 and DATEVALUE(05-01-1900)=5. I want a similar function in R, to obtain que amount of days between one date and another.

  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. It's unclear to me from your description what you are trying to do. – MrFlick Nov 06 '19 at 19:36
  • 1
    Base R supports taking the difference of objects of class `Date`. `a <- as.Date("2019/01/01"); b <- as.Date("2019/01/05"); b - a` The `lubridate` library extends this functionality. – Mako212 Nov 06 '19 at 19:46
  • 1
    It looks like you are using format dd-mm-yyyy. Using the format yyyy-mm-dd will probably make life easiest. If not, you will need to use `format = %d-%m-%Y` within the `as.Date` function to read correctly. –  Nov 06 '19 at 23:08

2 Answers2

2

How about something like this?

bone_df <- data.frame(date=c("2019-07-26","2019-07-25"),bone_start=c("2019-01-01","2019-01-01"))

bone_df$date_value <- as.Date(as.character(bone_df$date), format="%Y-%m-%d")-
  as.Date(as.character(bone_df$bone_start), format="%Y-%m-%d")

bone_df

        date bone_start date_value
1 2019-07-26 2019-01-01   206 days
2 2019-07-25 2019-01-01   205 days
Aswiderski
  • 166
  • 9
1

An equivalent to DATEVALUE() would be just

date <- as.Date("1900-01-01")

as.numeric(date)

# Output
[1] -25567

It gives a negative number because R date reference is 1970-01-01. So as.numeric(as.Date("1970-01-01")) outputs 0.

If you want the difference in days between two dates:

date1 <- as.Date("2000-01-01")

date2 <- as.Date("2019-11-06")

date2 - date1
# Output: Time difference of 7249 days (class: difftime, type: double)

as.numeric(date2 - date1)
# Output: 7249

Take a look at lubridate package for a set of nice tools to work with date-times.

Gabriel M. Silva
  • 642
  • 4
  • 10