Skip to content

Instantly share code, notes, and snippets.

@eldiabloavis
Created May 22, 2019 16:58
Show Gist options
  • Save eldiabloavis/ae59beb1e4d7b609390c85f98fa0578e to your computer and use it in GitHub Desktop.
Save eldiabloavis/ae59beb1e4d7b609390c85f98fa0578e to your computer and use it in GitHub Desktop.
---
title: "David's code"
author: "SILAKSH"
date: "22 May 2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(xts)
```
##Import CSV
```{r load input}
input <- read.csv("Indoormoi2.csv",stringsAsFactors = F)
input$Date <- ( as.POSIXct(input$Date, format= "%Y-%m-%d %H:%M:%S"))
dim(input)
summary(input)
```
##Your original code logic
```{r logic of david}
minutes <- cut(input$Date,breaks = "60 min")
pm1 <- with(input,unname(tapply(PM1,minutes,tail,1)))
head(pm1)
```
I tried your logic and it seems to have no issues here. I do not know why you had such an error.
## Slightly Modified logic
Similar to what I mentioned in my comment, here I am generating an index based on the same logic that you had used.
```{r logic of siva}
index <- as.numeric(tapply(1:length(minutes),minutes,tail,1))
output <- input[index,]
head(output)
```
## Alternate Approach
Since you are working with timeseries data, I would suggest trying out the xts package. Here I have shown an example.
```{r alternate logic}
newinput <- (xts(x = input[,-1],order.by = input$Date))
head(newinput)
newoutput <- to.hourly(newinput$PM1)
head(newoutput)
```
Here the aggregation over time period is done in OHLC form. Open is the first value of the period and Close is the last value of the period. High and Low are max and min within the period respectively.
Hope this helps. Have Fun!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment