Filter data at the date end of the year

Hello everybody,

I would like to ask for help. I have data like that.

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-12-15", "2010-12-16", "2010-12-19",
                              "2010-12-20", "2011-12-21", "2011-12-22",
                              "2011-12-23", "2011-12-26", "2010-12-27", "2010-12-28"))
)

I would like to filter the last day's stock price of each stock. My output is something like that.

image

I would appreciate any help.
Best regards,

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-12-15", "2010-12-16", "2010-12-19",
                                     "2010-12-20", "2011-12-21", "2011-12-22",
                                     "2011-12-23", "2011-12-26", "2010-12-27", "2010-12-28"))
)

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
data <- data %>% mutate(date3 = as.Date(date3)) 
MaxDates <- data %>%  group_by(stock) %>% summarize(MaxDate = max(date3))
data2 <- semi_join(data, MaxDates, by = c("stock", "date3" = "MaxDate"))
data2
#>      code year closed_price stock      date3
#> 1 AAA2010 2010        45200   AAA 2010-12-20
#> 2 AAC2011 2011        46100   AAC 2011-12-26
#> 3 AAD2010 2010        45800   AAD 2010-12-28

Created on 2019-11-27 by the reprex package (v0.2.1)

1 Like

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

Many thanks for quick help! I really appreciate.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.