Missing value when using max(date)

Dear all,
I would like to ask for help. I run this code for my stock data with 617 stocks, but it has some problems with the output. In several cases, it doesn't extract the max day of the month. It only works well only when I filter by each stock. Do you know what is the problem with this? Thank you in advance.

This is my code.
price_month <-price%>% mutate(date = as.Date(date)) %>% group_by(year,mm) %>% filter(date == max(date))

The above code only works well when I filter each stock like that
price11<-filter(price1, stock == "PTC")

The class of each variable is in the picture below.

Hi @sirius1170,
You need to add stock to your group_by function:

price_month  <- price %>% 
                mutate(date = as.Date(date)) %>% 
                group_by(stock, year, mm) %>% 
                filter(date == max(date))

HTH

1 Like

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

Thank you so much for your quick support.
Best regards,