Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>We can read it using <code>read.csv</code>, convert the first column to a date time binned into 6 minute intervals and add a dummy column of 1's. Then re-read it using <code>read.zoo</code> splitting on the type and aggregating on the dummy column:</p> <pre><code># test data Lines &lt;- 'date,type "Sep 22, 2011 12:54:53.081240000","2" "Sep 22, 2011 12:54:53.083493000","2" "Sep 22, 2011 12:54:53.084025000","2" "Sep 22, 2011 12:54:53.086493000","2" "Sep 22, 2011 12:54:53.081240000","3" "Sep 22, 2011 12:54:53.083493000","3" "Sep 22, 2011 12:54:53.084025000","3" "Sep 22, 2011 12:54:53.086493000","4"' library(zoo) library(chron) # convert to chron and bin into 6 minute bins using trunc # Also add a dummy column of 1's # and remove any leading space (removing space not needed if there is none) DF &lt;- read.csv(textConnection(Lines), as.is = TRUE) fmt &lt;- '%b %d, %Y %H:%M:%S' DF &lt;- transform(DF, dummy = 1, date = trunc(as.chron(sub("^ *", "", date), format = fmt), "00:06:00")) # split and aggregate z &lt;- read.zoo(DF, split = 2, aggregate = length) </code></pre> <p>With the above test data the solution looks like this:</p> <pre><code>&gt; z 2 3 4 (09/22/11 12:54:00) 4 3 1 </code></pre> <p>Note that the above has been done in wide form since that form constitutes a time series whereas the long form does not. There is one column for each type. In our test data we had types 2, 3 and 4 so there are three columns.</p> <p>(We have used chron here since its <code>trunc</code> method fits well with binning into 6 minute groups. chron does not support time zones which can be an advantage since you can't make one of the many possible time zone errors but if you want POSIXct anyways convert it at the end, e.g. <code>time(z) &lt;- as.POSIXct(paste(as.Date.dates(time(z)), times(time(z)) %% 1))</code> . This expression is shown in a table in one of the R News 4/1 articles except we used <code>as.Date.dates</code> instead of just <code>as.Date</code> to work around a bug that seems to have been introduced since then. We could also use <code>time(z) &lt;- as.POSIXct(time(z))</code> but that would result in a different time zone.)</p> <p>EDIT:</p> <p>The original solution binned into dates but I noticed afterwards that you wish to bin into 6 minute periods so the solution was revised.</p> <p>EDIT:</p> <p>Revised based on comment.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload