Drew
Drew

Reputation: 3

Loop Issue in Mathematica

I am currently attempting to run a loop in Mathematica which will attempt to insert in position {i,4} of the date list the day of the week. For some reason i can't get dayint to increase when date[[i,3]]!=date[[i-1,3]] and all values of date[[i,4]]=5. I would much appreciate any insight available on this issue.

In[4]:= n = 344674; dayint = 5;

In[5]:= solardata = 
  Import["U:\\Masters Project\\Hobo \
Data\\SORMS_Landfill_Comparison_Input.csv", "csv"];

In[6]:= date = 
 Table[DateList[{ToString[solardata[[i, 1]]], {"Month", "Day", 
     "YearShort"}}], {i, n}];

In[8]:= date[[1, 4]] = 5;

In[14]:= For[i = 2, i < n + 1, i++, 
 If[date[[i, 3]] == date[[i - 1, 3]], date[[i, 4]] = dayint, 
  If[dayint == 7, dayint = 1, dayint++]; date[[i, 4]] = dayint]];

In[17]:= date;

Upvotes: 0

Views: 104

Answers (1)

Chris Degnen
Chris Degnen

Reputation: 8655

Seems to work ok.

n = 3; dayint = 5;

date = {
   {2013, 11, 30, 0, 0, 0.},
   {2013, 11, 30, 0, 0, 0.},
   {2013, 12, 01, 0, 0, 0.}};

For[i = 2, i < n + 1, i++,
  If[date[[i, 3]] == date[[i - 1, 3]],
   date[[i, 4]] = dayint,
   If[dayint == 7, dayint = 1, dayint++];
   date[[i, 4]] = dayint]];

date

{{2013, 11, 30, 0, 0, 0.}, {2013, 11, 30, 5, 0, 0.}, {2013, 12, 1, 6, 0, 0.}}

Edit

Perhaps you have missing dates?

date = {
   {2013, 11, 28, 0, 0, 0.},
   {2013, 11, 29, 0, 0, 0.},
   {2013, 11, 29, 0, 0, 0.},
   {2013, 11, 30, 0, 0, 0.},
   {2013, 11, 30, 0, 0, 0.},
   {2013, 12, 01, 0, 0, 0.}};

dates = Union@date;
{mindate, maxdate} = Through[{First, Last}@dates];
days = QuantityMagnitude@DateDifference[mindate, maxdate, "Day"];
If[Length[dates] == days + 1, "All dates included", "There are dates missing"]

All dates included

Upvotes: 1

Related Questions