---
# System prepended metadata

title: Rscript to exclude or include specific hours
tags: [Rscripts]

---

---
tags: Rscripts
---

# Rscript to exclude or include specific hours

```
target <- c(0,1,2,3,4,5,6)
cages <- feb20_3 %>% filter(animal %in% target)
cages <- with(feb20_3 ,feb20_3[ hour( StartTime ) <= 5  | hour( StartTime ) >= 10  | hour( StartTime ) >= 10 , ] )
cages <- with(cages ,cages[ hour( StartTime ) <= 19   | hour( StartTime ) >= 22 , ] )
measurement <- cages %>%  select(metric)
df<-as.data.frame(measurement[[metric]])
legend_title <- "Cage Number"
p <- ggplot(data = cages,aes(x=as.POSIXct(StartTime),y=measurement[[metric]]))
p <- p + geom_point(aes(group=as.factor(animal), color=as.factor(animal)), size = 3)
p <- p + theme_grey(base_size = 15)
p <- p + geom_smooth(data=df$V1, method='loess', span=.9)
p <- p + labs(x = "", y = metric)
p <- p + scale_color_brewer(legend_title, palette="Paired")
p <- p + scale_x_datetime(date_breaks = "2 hours", date_labels = "%H:%M")
#p <- p + geom_hline(yintercept = 0.8907387)
#p <- p + geom_line(aes(y =cages$Deg_C))
p

```
![](https://i.imgur.com/UKrjWxs.png)

### to select out a slice

```
target <- c(0,1,2,3,4,5,6)
cages <- feb20_3 %>% filter(animal %in% target)
cages <- with( cages ,cages[ hour( StartTime ) >= 0 & hour( StartTime ) < 4 , ] )

measurement <- cages %>%  select(metric)
df<-as.data.frame(measurement[[metric]])
legend_title <- "Cage Number"

p <- ggplot(data = cages,aes(x=as.POSIXct(StartTime),y=measurement[[metric]]))
p <- p + geom_point(aes(group=as.factor(animal), color=as.factor(animal)), size = 3)
p <- p + theme_grey(base_size = 15)
p <- p + geom_smooth(data=df$V1, method='loess', span=.9)
p <- p + labs(x = "", y = metric)
p <- p + scale_color_brewer(legend_title, palette="Paired")
p <- p + scale_x_datetime(date_breaks = "2 hours", date_labels = "%H:%M")
#p <- p + geom_hline(yintercept = 0.8907387)
p

```

![](https://i.imgur.com/bHqSqFD.png)




