Taking the Differences Between 2 Dates (R)

Noob

I am trying to take the difference between two dates in R. Normally, I could have just done this in MS Excel, but I want to try and learn something new by doing this in R. The dates are in the form : Year-Month-Date and are in "factor" format:

# how the data looks like
d <- data.frame (

"day_a" = c("2010-12-25", "2020-10-31"),
"day_b" = c("2011-12-24", "2021-01-01")

)

d$day_a = as.factor(d$day_a)
d$day_b = as.factor(d$day_b)

First, I tried to do a straightforward subtraction :

   #my first attempt to take the difference in days
    d$day_1 = as.Date(d$day_a, "%Y/%m/%d")
    d$day_2 = as.Date(d$day_b, "%Y/%m/%d")
    
    d$diff = d$day_1 - d$day_2

Then, I tried to use the "lubdridate" library:

library(lubridate)
d$diff=interval(ymd(d$day_1),ymd(d$day_2))

However, this also did not work.

Could someone please tell me what I am doing wrong?

Thanks

Duck

You need a character input for dates. Also, you were using %Y/%m/%d and you defined in d data as Y-m-d so that the date conversion would work fine:

d <- data.frame (
  
  "day_a" = c("2010-12-25", "2020-10-31"),
  "day_b" = c("2011-12-24", "2021-01-01")
  
)

d$day_a = as.factor(d$day_a)
d$day_b = as.factor(d$day_b)

d$day_1 = as.Date(as.character(d$day_a))
d$day_2 = as.Date(as.character(d$day_b))

d$diff = d$day_1 - d$day_2

Output:

d
       day_a      day_b      day_1      day_2      diff
1 2010-12-25 2011-12-24 2010-12-25 2011-12-24 -364 days
2 2020-10-31 2021-01-01 2020-10-31 2021-01-01  -62 days

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

differences between dates in sql

분류에서Dev

Search between 2 dates in webmatrix

분류에서Dev

how to get a column with a list of dates between 2 known dates

분류에서Dev

How to count fractional days between 2 dates

분류에서Dev

Calculating Time Difference between 2 Dates

분류에서Dev

Excel get value between 2 dates

분류에서Dev

Reading logs with the word 'ERROR' between 2 dates

분류에서Dev

R - How to subset a table between two specific dates?

분류에서Dev

how to receive all days between 2 dates in laravel

분류에서Dev

Number of working days between 2 dates according JIRA's configuration

분류에서Dev

What are the differences between Mythbuntu and MythTV?

분류에서Dev

Differences between volume, partition and drive

분류에서Dev

Differences between Linux Distribution for Meteor

분류에서Dev

setting up apache2 webserver -- differences between behaviour within my intranet and outside

분류에서Dev

How to get difference between 2 dates in number of days,hours,minutes and seconds

분류에서Dev

Differences between ping and ping -c 3 option

분류에서Dev

Differences between keyword, reserved word, and builtin?

분류에서Dev

Ownership: differences between tuples and arrays in Rust

분류에서Dev

What security differences are between click and .deb package?

분류에서Dev

What are the differences between XMLHttpRequest and ServletRequest and their responses?

분류에서Dev

detect differences between two strings with Javascript

분류에서Dev

Any differences between identify --verbose and exiftool?

분류에서Dev

Differences between `su` and `su-to-root`?

분류에서Dev

What are the differences between the various partition tables?

분류에서Dev

Difference in years between two dates

분류에서Dev

Twig, subtract 2 dates

분류에서Dev

comparing 2 dates with javascript

분류에서Dev

How to display all the dates between multiple two dates in a table?

분류에서Dev

Taking the difference between current and previous data via Access VBA

Related 관련 기사

  1. 1

    differences between dates in sql

  2. 2

    Search between 2 dates in webmatrix

  3. 3

    how to get a column with a list of dates between 2 known dates

  4. 4

    How to count fractional days between 2 dates

  5. 5

    Calculating Time Difference between 2 Dates

  6. 6

    Excel get value between 2 dates

  7. 7

    Reading logs with the word 'ERROR' between 2 dates

  8. 8

    R - How to subset a table between two specific dates?

  9. 9

    how to receive all days between 2 dates in laravel

  10. 10

    Number of working days between 2 dates according JIRA's configuration

  11. 11

    What are the differences between Mythbuntu and MythTV?

  12. 12

    Differences between volume, partition and drive

  13. 13

    Differences between Linux Distribution for Meteor

  14. 14

    setting up apache2 webserver -- differences between behaviour within my intranet and outside

  15. 15

    How to get difference between 2 dates in number of days,hours,minutes and seconds

  16. 16

    Differences between ping and ping -c 3 option

  17. 17

    Differences between keyword, reserved word, and builtin?

  18. 18

    Ownership: differences between tuples and arrays in Rust

  19. 19

    What security differences are between click and .deb package?

  20. 20

    What are the differences between XMLHttpRequest and ServletRequest and their responses?

  21. 21

    detect differences between two strings with Javascript

  22. 22

    Any differences between identify --verbose and exiftool?

  23. 23

    Differences between `su` and `su-to-root`?

  24. 24

    What are the differences between the various partition tables?

  25. 25

    Difference in years between two dates

  26. 26

    Twig, subtract 2 dates

  27. 27

    comparing 2 dates with javascript

  28. 28

    How to display all the dates between multiple two dates in a table?

  29. 29

    Taking the difference between current and previous data via Access VBA

뜨겁다태그

보관