Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since you're not asking about the time (hourly) part of your data, it is best to then store your data as a Date object. Otherwise, you might be interested in <em>chron</em>, which also has some convenience functions like you'll see below.</p> <p>With respect to Conjugate Prior's answer, you should store your date data as a Date object. Since your data already follows the default format ('yyyy-mm-dd') you can just call as.Date on it. Otherwise, you would have to specify your string format. I would also use as.character on your factor to make sure you don't get errors inline. I know I've ran into problems with factors-into-Dates for that reason (possibly corrected in current version). </p> <pre><code>df.MHwind_load &lt;- transform(df.MHwind_load, Date = as.Date(as.character(Date))) </code></pre> <p>Now you would do well to create wrapper functions that extract the information you desire. You could use <em>transform</em> like I did above to simply add those columns that represent months, days, years, etc, and then subset on them logically. Alternatively, you might do something like this:</p> <pre><code>getMonth &lt;- function(x, mo) { # This function assumes w/in single year vector isMonth &lt;- month(x) %in% mo # Boolean of matching months return(x[which(isMonth)] # Return vector of matching months } # end function </code></pre> <p>Or, in short form</p> <pre><code>getMonth &lt;- function(x, mo) x[month(x) %in% mo] </code></pre> <p>This is just a tradeoff between storing that information (transform frame) or having it processed when desired (use accessor methods). </p> <p>A more complicated process is your need for, say, the first day of a month. This is not entirely difficult, though. Below is a function that will return all of those values, but it is rather simple to just subset a sorted vector of values for a given month and take their first one.</p> <pre><code>getFirstDay &lt;- function(x, mo) { isMonth &lt;- months(x) %in% mo x &lt;- sort(x[isMonth]) # Look at only those in the desired month. # Sort them by date. We only want the first day. nFirsts &lt;- rle(as.numeric(x))$len[1] # Returns length of 1st days return(x[seq(nFirsts)]) } # end function </code></pre> <p>The easier alternative would be</p> <pre><code>getFirstDayOnly &lt;- function(x, mo) {sort(x[months(x) %in% mo])[1]} </code></pre> <p>I haven't prototyped these, as you didn't provide any data samples, but this is the sort of approach that can help you get the information you desire. It is up to you to figure out how to put these into your work flow. For instance, say you want to get the first day for each month of a given year (assuming we're only looking at one year; you can create wrappers or pre-process your vector to a single year beforehand). </p> <pre><code># Return a vector of first days for each month df &lt;- transform(df, date = as.Date(as.character(date))) sapply(unique(months(df$date)), # Iterate through months in Dates function(month) {getFirstDayOnly(df$date, month)}) </code></pre> <p>The above could also be designed as a separate convenience function that uses the other accessor function. In this way, you create a series of direct but concise methods for getting pieces of the information you want. Then you simply pull them together to create very simple and easy to interpret functions that you can use in your scripts to get you precise what you desire in the most efficient manner. </p> <p>You should be able to use the above examples to figure out how to prototype other wrappers for accessing the date information you require. If you need help on those, feel free to ask in a comment. </p>
    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. 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.
 

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