Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the process that may help you most is to mutate your [TIMESTAMP] data into a grouping variable. Then, I recommend using one of the many data summary packages to create the report. My personal preference is to use the 'plyr' package for both tasks, and I use it in this example.</p> <p><strong>Step 1</strong>: Use the 'as.POSIXct' function to convert your timestamp data to POSIX datetimes for use with the various datetime functions. Use no parameters to simply convert the data without any adjustments.</p> <pre><code>data$TIMESTAMP &lt;- as.POSIXct(data$TIMESTAMP) </code></pre> <p><strong>Update</strong>: Since the time is in not in the unambiguous, decreasing-order format (i.e. YYYY/MM/DD HH:MM:SS), the 'as.POSIXct' function won't be able to do a quick conversion of the data. Use 'as.POSIXct' only when you are using the unambiguous format. For other arrangements, use the 'strptime' function, specifying the current format like so:</p> <pre><code>data$TIMESTAMP &lt;- strptime(data$TIMESTAMP, "%m/%d/%Y %H:%M") </code></pre> <p>This tells the 'strptime' function what format is currently in use, and exports a POSIX-compatible datetime. There should not be any need to use the 'as.character' function, unless your current data is not a string.</p> <p><strong>Step 2</strong>: Use 'plyr' function 'ddply' (takes dataframe and returns dataframe) to create a new variable for use in the groupings. Use the 'format' function to extract the data you want from the TIMESTAMP values. Look at the 'format' documentation for available formats. In this case, here is how you would create a [MONTH] variable:</p> <pre><code>library(plyr) data &lt;- ddply(data, .(TIMESTAMP), mutate, MONTH=format(TIMESTAMP, "%m") </code></pre> <p><strong>Step 3</strong>: Use 'plyr' function 'ddply' to summarize the data by your new variable.</p> <pre><code>ddply(data, .(MONTH), summarize, V1_AVG=mean(V2), V2_AVG=mean(V2)) </code></pre> <p>If you wanted to also summarize by a second variable (like [GROUP]), just include that in the second function variable, like so:</p> <pre><code>ddply(data, .(MONTH, GROUP), summarize, V1_AVG=mean(V2), V2_AVG=mean(V2)) </code></pre> <p>Technically, you could do this all in one statement, but experience has taught me caution. I recommend doing each step by itself to make sure nothing gets messed up.</p> <p>You can parse your data however you like by fiddling around like this just as long as your timestamps have been converted to POSIX datetimes. The 'plyr' package is extremely flexible for stuff like this.</p> <p><strong>Update</strong>: As per the OP's request, I am including how you would do the same calculation but using only data between the hours of 12p and 4p. You don't actually have to use any particular package to subset your data like this, since it's a straight data filter. Just change the data set inputted into the 'ddply' function like so:</p> <pre><code># Use one of the following lines, which both do the same thing. # I'm just including both as different examples of logic that can be used. data_Subset &lt;- data[format(data$TIMESTAMP, "%H") &gt;= 12 &amp; format(data$TIMESTAMP, "%H") &lt; 16,] data_Subset &lt;- data[format(data$TIMESTAMP, "%H") %in% 12:15,] # Then summarize using the new data frame as an input ddply(data_Subset, .(MONTH, GROUP), summarize, V1_AVG=mean(V2), V2_AVG=mean(V2)) </code></pre> <p>Here, we are filtering the data frame to only show rows (with all columns) where the hour (%H) is equal to 12 through 15. This effectively includes all times from 12:00 to 15:59. If you start getting into very large data sets, you may have to look for other solutions (like the 'data.table' package), but otherwise, this is your fastest option.</p> <p>Again, this only works, because we have transformed our datetimes into POSIX-compatible datetimes.</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. 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