1) zoo You can create a zoo series without duplicates by using read.zoo
and specifying an aggregate
function like this. In the example we assumed you want the last of any duplicates but we can use mean
, median
, function(x) head(x, 1)
or other functions for other aggregates.
library(zoo)
z <- read.zoo(DF, format = "%m/%d/%Y", aggregate = function(x) tail(x, 1))
Now plot(z)
, lattice::xyplot(z)
or ggplot2::autoplot(z)
will plot it, fortify.zoo(z)
will convert it to a data frame, etc.
2) base We can use aggregate
in base like this:
DF2 <- transform(DF, Date = as.Date(Date, "%m/%d/%Y"))
aggregate(Hrs ~ Date, DF2, function(x) tail(x, 1))
or we can use any of the aggregate functions mentioned in (1).
## Note
Lines <- " Date Hrs
6/14/2018 364.8
6/15/2018 372.6
6/15/2018 381.9
6/21/2018 383.3
6/22/2018 394.5
6/25/2018 411
6/28/2018 423.9
6/28/2018 424.9"
DF <- read.table(text = Lines, header = TRUE)