We can use dplyr
library(dplyr)
library(lubridate)
library(zoo)
If we need only the last day of month and not the last day that is found in the dataset
df %>%
filter(dmy(V2) == as.Date(as.yearmon(dmy(V2)), frac=1))
# V1 V2
#1 4.98 31/01/2001
But, if we need to filter the last day found in the dataset for each month
df %>%
mutate(V3 = dmy(V2))%>%
group_by(month = month(V3), year = year(V3)) %>%
slice(which.max(day(V3))) %>%
ungroup() %>%
select(-month, -year, -V3)
# V1 V2
# <dbl> <chr>
#1 4.98 31/01/2001
#2 4.59 29/12/2000
If it is only grouped by 'month', just remove the year = year(V3))
in the group_by
and we will get
df %>%
mutate(V3 = dmy(V2))%>%
group_by(month = month(V3)) %>%
slice(which.max(day(V3))) %>%
ungroup() %>%
select(-month, -V3)
data
df <- structure(list(V1 = c(4.59, 4.59, 4.58, 4.52, 4.54, 4.58, 4.09,
4.5, 4.18, 4.11, 3.54, 4.98), V2 = c("29/12/2000", "01/01/2001",
"02/01/2001", "03/01/2001", "04/01/2001", "05/01/2001", "26/01/2001",
"27/01/2001", "28/01/2001", "29/01/2001", "30/01/2001", "31/01/2001"
)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA,
-12L))