Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't know why you'd use lubridate for this. If you're just looking for something less awesome than xts you could try this</p> <pre><code>tapply(bikecounts$Northbound, as.Date(bikecounts$Date, format="%m/%d/%Y"), sum) </code></pre> <p>Basically, you just need to <code>split</code> by Date, then apply a function.</p> <hr> <p>lubridate could be used for creating a grouping factor for split-apply problems. So, for example, if you want the sum for each month (ignoring year)</p> <pre><code>tapply(bikecounts$Northbound, month(mdy_hms(bikecounts$Date)), sum) </code></pre> <p>But, it's just using wrappers for base R functions, and in the case of the OP, I think the base R function <code>as.Date</code> is the easiest (as evidenced by the fact that the other Answers also ignored your request to use lubridate ;-) ).</p> <hr> <p>Something that wasn't covered by the <a href="https://stackoverflow.com/a/15794871">Answer</a> to the other <a href="https://stackoverflow.com/questions/15794710/data-frames-in-r">Question</a> linked to in the OP is <code>split.xts</code>. <code>period.apply</code> splits an <code>xts</code> at <code>endpoints</code> and applies a function to each group. You can find endpoints that are useful for a given task with the <code>endpoints</code> function. For example, if you have an xts object, <code>x</code>, then <code>endpoints(x, "months")</code> would give you the row numbers that are the last row of each month. <code>split.xts</code> leverages that to split an xts object -- <code>split(x, "months")</code> would return a list of xts objects where each component was for a different month.</p> <p>Although, <code>split.xts()</code> and <code>endpoints()</code> are primarily intended for <code>xts</code> objects, they also work on some other objects as well, including plain time based vectors. Even if you don't want to use xts objects, you still may find uses for <code>endpoints()</code> because of its convenience or its speed (implemented in C)</p> <pre><code>&gt; split.xts(as.Date("1970-01-01") + 1:10, "weeks") [[1]] [1] "1970-01-02" "1970-01-03" "1970-01-04" [[2]] [1] "1970-01-05" "1970-01-06" "1970-01-07" "1970-01-08" "1970-01-09" [6] "1970-01-10" "1970-01-11" &gt; endpoints(as.Date("1970-01-01") + 1:10, "weeks") [1] 0 3 10 </code></pre> <p>I think lubridate's best use in this problem is for parsing the "Date" strings into POSIXct objects. i.e. the <code>mdy_hms</code> function in this case.</p> <p>Here's an <code>xts</code> solution that uses <code>lubridate</code> to parse the "Date" strings.</p> <pre><code>x &lt;- xts(bikecounts[, -1], mdy_hms(bikecounts$Date)) period.apply(x, endpoints(x, "days"), sum) apply.daily(x, sum) # identical to above </code></pre> <p>For this specific task, <code>xts</code> also has an optimized <code>period.sum</code> function (written in Fortran) that is very fast</p> <pre><code>period.sum(x, endpoints(x, "days")) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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