Filter data at the date end of the year

Another way to do it

library(dplyr)

data <- data.frame(stringsAsFactors=FALSE,
                 code = c("AAA2010", "AAA2010", "AAA2010", "AAA2010", "AAC2011",
                          "AAC2011", "AAC2011", "AAC2011", "AAD2010", "AAD2010"),
                 year = c("2010", "2010", "2010", "2010", "2011", "2011", "2011",
                          "2011", "2010", "2010"),
                 closed_price = c(49900L, 46600L, 46900L, 45200L, 45100L, 45600L, 46500L,
                                  46100L, 46100L, 45800L),
                 stock = as.factor(c("AAA", "AAA", "AAA", "AAA", "AAC", "AAC", "AAC",
                                     "AAC", "AAD", "AAD")),
                 date3 = as.factor(c("2010-07-15", "2010-07-16", "2010-07-19",
                                     "2010-07-20", "2011-08-21", "2011-08-22",
                                     "2011-08-23", "2011-08-26", "2010-07-27", "2010-07-28"))
)

data %>% 
    mutate(date3 = as.Date(date3)) %>% 
    group_by(code) %>% 
    filter(date3 == max(date3))
#> # A tibble: 3 x 5
#> # Groups:   code [3]
#>   code    year  closed_price stock date3     
#>   <chr>   <chr>        <int> <fct> <date>    
#> 1 AAA2010 2010         45200 AAA   2010-07-20
#> 2 AAC2011 2011         46100 AAC   2011-08-26
#> 3 AAD2010 2010         45800 AAD   2010-07-28

Note: For future posts please use proper code formatting as explained here (I already edited your post to do so)

2 Likes