Answers.Body: <p>I think your error is located in your use of <code>as.POSIXct</code> which gives you wrong date formats because you did not provide a format in your function:</p>
  
  <pre class="lang-r prettyprint-override"><code>&gt; as.POSIXct("27/11/2012")
  [1] "27-11-20 LMT"
  
  &gt; as.POSIXct("27/11/2012", format = "%d/%m/%Y")
  [1] "2012-11-27 PST"
  </code></pre>
  
  <p>So, you need to first convert your date in the appropriate date format and then you will be able to calculate the time interval. </p>
  
  <p>Personally, I prefer to manipulate dates with <code>lubridate</code> package because the name of their function is a little bit more intuitive to me. For example, if you have a date to the format "Day/Month/Year", you can use <code>dmy</code> function. </p>
  
  <p>With your example, you can do:</p>
  
  <pre class="lang-r prettyprint-override"><code>library(lubridate)
  library(dplyr)
  
  df &lt;- data.frame(Date.Death = c("02/08/2015","02/05/2015"),
                   Date.Operation = c("27/11/2012","27/11/2012"))
  
  df %&gt;% mutate_all(~dmy(.)) %&gt;% 
    mutate(Diff_Weeks = interval(Date.Operation,Date.Death) / weeks(1),
                                        Diff_days = Date.Death - Date.Operation)
  
    Date.Death Date.Operation Diff_Weeks Diff_days
  1 2015-08-02     2012-11-27   139.7143  978 days
  2 2015-05-02     2012-11-27   126.5714  886 days
  <span class="math-container">```</span>
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-05T14:42:21.867Z
Answers.Text: Sorry why inviting me? :( I googled a lot but I did not find the solution
Answers.UserDisplayName: Zizogolu
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-05T04:13:27.917Z
Answers.LastActivityDate: 2020-04-05T04:13:27.917Z
Answers.UserDisplayName: dc37
Body: <p>I have a very big clinical data like</p>
  
  <pre><code>Date.Death  Date Of Operation
  02/08/2015  27/11/2012
  02/08/2015  27/11/2012
  </code></pre>
  
  <p>I want to know how many weeks each patient has lived after operation</p>
  
  <p>I then tried </p>
  
  <pre><code>&gt; t1 &lt;- as.POSIXct("27/11/2012")
  &gt; t2 &lt;- as.POSIXct("02/08/2015")
  &gt; as.double(difftime(t2,t1,unit="weeks"))
  [1] -1317.571
  </code></pre>
  
  <p>Or</p>
  
  <pre><code>&gt; span &lt;- interval(as.POSIXct("27/11/2012"), as.POSIXct("02/08/2015"))
  &gt; span
  [1] 0027-11-20 LMT--0002-08-20 LMT
  &gt; t &lt;- as.period(span, unit="day")
  &gt; t
  [1] "-9223d 0H 0M 0S"
  &gt; t / as.period(dweeks(1))
  estimate only: convert to intervals for accuracy
  [1] -1317.571
  </code></pre>
  
  <p>Both give wrong number of weeks</p>
  
  <p>Manually calculating one by one is painful and error prone </p>
  
  <p>You know anyway to get the number of weeks?</p>
  
  <p>Thanks</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-06T09:46:22.913Z
Comments.Text: I'm voting to close this question as off-topic because it has nothing to do with bioinformatics.
Comments.UserDisplayName: terdon
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-06T14:53:09.390Z
Comments.Text: https://www.statmethods.net/input/dates.html
Comments.UserDisplayName: Kamil S Jaron
Tags: <r>
Title: Getting the number of weeks from a time span
ThreadText: # Getting the number of weeks from a time span
  
  (Asked by: username_0 on 2020-04-05)
  
  I have a very big clinical data like
  
  ```
  Date.Death  Date Of Operation
  02/08/2015  27/11/2012
  02/08/2015  27/11/2012
  
  ```
  
  I want to know how many weeks each patient has lived after operation
  
  I then tried
  
  ```
  > t1 <- as.POSIXct("27/11/2012")
  > t2 <- as.POSIXct("02/08/2015")
  > as.double(difftime(t2,t1,unit="weeks"))
  [1] -1317.571
  
  ```
  
  Or
  
  ```
  > span <- interval(as.POSIXct("27/11/2012"), as.POSIXct("02/08/2015"))
  > span
  [1] 0027-11-20 LMT--0002-08-20 LMT
  > t <- as.period(span, unit="day")
  > t
  [1] "-9223d 0H 0M 0S"
  > t / as.period(dweeks(1))
  estimate only: convert to intervals for accuracy
  [1] -1317.571
  
  ```
  
  Both give wrong number of weeks
  
  Manually calculating one by one is painful and error prone
  
  You know anyway to get the number of weeks?
  
  Thanks
  
  **Tags:** r
  
  > **Comments:**
  > **username_1**: I'm voting to close this question as off-topic because it has nothing to do with bioinformatics.  
  > **username_2**: https://www.statmethods.net/input/dates.html  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2020-04-05. Score: 2 | Accepted answer
  
  I think your error is located in your use of `as.POSIXct` which gives you wrong date formats because you did not provide a format in your function:
  
  ```r
  > as.POSIXct("27/11/2012")
  [1] "27-11-20 LMT"
  
  > as.POSIXct("27/11/2012", format = "%d/%m/%Y")
  [1] "2012-11-27 PST"
  
  ```
  
  So, you need to first convert your date in the appropriate date format and then you will be able to calculate the time interval.
  
  Personally, I prefer to manipulate dates with `lubridate` package because the name of their function is a little bit more intuitive to me. For example, if you have a date to the format "Day/Month/Year", you can use `dmy` function.
  
  With your example, you can do:
  
  ```r
  library(lubridate)
  library(dplyr)
  
  df <- data.frame(Date.Death = c("02/08/2015","02/05/2015"),
                   Date.Operation = c("27/11/2012","27/11/2012"))
  
  df %>% mutate_all(~dmy(.)) %>% 
    mutate(Diff_Weeks = interval(Date.Operation,Date.Death) / weeks(1),
                                        Diff_days = Date.Death - Date.Operation)
  
    Date.Death Date.Operation Diff_Weeks Diff_days
  1 2015-08-02     2012-11-27   139.7143  978 days
  2 2015-05-02     2012-11-27   126.5714  886 days
  ```
  
  ```
  
  > **Comments:**
  > **username_0**: Sorry why inviting me? :( I googled a lot but I did not find the solution  
  
  ---
  
  