0
$\begingroup$

I know it's a beginner question, but I've struggled with it for some time now and can't find a solution.

I have a dataset consisting of timestamps, where I've managed to compute the time differences with the help of a for loop:

For[i = 1; t = 0, i < 3, i++,  
  t = 
    UnitConvert[
      DateDifference[
        DateObject[sessions5311[i, "datetimeaccess"]], 
        DateObject[sessions5311[i + 1, "datetimeaccess"]]], 
      "Minutes" ]; 
  Print[t]]

sessions5311 = sessions5311[All, Append[#, "Differences" -> t] &]

When I try to append it, only the last value of t is being inserted in the "Differences" column, but I would want to have each of the current values of the t variable during the computation there.

enter image description here

Your help is much appreciated :).

$\endgroup$
  • 1
    $\begingroup$ Better use Table instead of For + Print. Print is meant only for display, not for returning values. $\endgroup$ – Henrik Schumacher Apr 14 at 22:59
0
$\begingroup$

Since you did not give us your dataset to work with, I contrived this one for exposition purposes.

ds = 
  Dataset[
    {Association["a" -> 1, "b" -> "x", 
      "date" -> DateObject[{2019, 4, 14, 20, 14, 47.767113}, "Instant", Gregorian", -4.]], 
     Association["a" -> 2, "b" -> "y", 
       "date" -> DateObject[{2019, 4, 15, 20, 14, 47.767451}, "Instant", "Gregorian", -4.]], 
     Association["a" -> 3, "b" -> "z", 
       "date" -> DateObject[{2019, 4, 16, 20, 14, 47.767678}, "Instant", "Gregorian", -4.]], 
     Association["a" -> 4, "b" -> "x", 
       "date" -> DateObject[{2019, 4, 17, 20, 14, 47.7679}, "Instant", "Gregorian", -4.]], 
     Association["a" -> 5, "b" -> "y", 
       "date" -> DateObject[{2019, 4, 18, 20, 14, 47.768123}, "Instant", "Gregorian", -4.]], 
     Association["a" -> 6, "b" -> "z", 
       "date" -> DateObject[{2019, 4, 19, 20, 14, 47.768346}, "Instant", "Gregorian", -4.]]}]; 

With this dataset, I can add a column of date differences given in minutes starting with the following code.

rows = Length @ ds;
dates = ds[All , "date"] // Normal
diffs = 
  Association["differce" -> #] & /@
    Prepend[
      Table[DateDifference[dates[[i]], dates[[i + 1]], "Minutes"], {i, rows - 1}],
      Quantity[0., "Minutes"]]

Evaluation gives a list of differences expressed as associations.

{<|"difference" -> Quantity[0., "Minutes"]|>, 
 <|"difference" -> Quantity[1440., "Minutes"]|>, 
 <|"difference" -> Quantity[1440., "Minutes"]|>, 
 <|"difference" -> Quantity[1440., "Minutes"]|>, 
 <|"difference" -> Quantity[1440., "Minutes"]|>, 
 <|"difference" -> Quantity[1440., "Minutes"]|>}

The list can be appended to ds as new column having the key "difference" with the following one-liner.

ds = Join @@@ Transpose @ {ds // Normal, diffs} // Dataset

ds

$\endgroup$
  • 1
    $\begingroup$ DateDifference accepts a unit as an optional third argument, so there is no need for the UnitConvert. $\endgroup$ – Rohit Namjoshi Apr 15 at 1:28
  • $\begingroup$ @RohitNamjoshi. Good tip. I have updated my code to use it. $\endgroup$ – m_goldberg Apr 15 at 1:38
  • $\begingroup$ Another way to generate the successive differences ds[All, "date"][ Differences][All, <|"Difference" -> UnitConvert[#, "Minutes"]|> &] $\endgroup$ – Rohit Namjoshi Apr 15 at 2:10
  • $\begingroup$ @RohitNamjoshi. It might be better if you would write up your alternative method as another answer. It would be more accessible that way. $\endgroup$ – m_goldberg Apr 15 at 2:36
  • $\begingroup$ Thank you both very much for your help, it worked now. $\endgroup$ – Lucy Apr 15 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.