Selecting the most recent date per country

Hello guys,

I have a problem which I can't seem to figure out. Im working with a dataset of about 500.000 observations over 4 variables. It looks like this:

1 1743-11-01 4.384 2.294 Netherlands
6 1744-04-01 1.530 4.680 Netherlands
7 1744-05-01 6.702 1.789 France
8 1744-06-01 11.609 1.577 France
9 1744-07-01 15.342 1.410 Germany
11 1744-09-01 11.702 1.517 Germany

Now what I am trying to do here is to filter the avarage temperature for the most recent date available, per country (the dataset has info of about 200 countries). The code I've tried is

Temperatures %>% group_by(Country) %>% filter(Date == max(Date))

The error I get while using this code is

Error in Summary.factor(c(1L, 6L, 7L, 8L, 9L, 11L, 12L, 13L, 14L, 15L, :
‘max’ not meaningful for factors

Any expert who can shed some light and help me out :slightly_smiling_face:

Thanks a lot in advance!!

It seems your Date column is actually a factor. You can use


to see if that is true. Try doing

Temperatures$Date <- as.Date(as.character(Temperatures$Date))

and then rerunning the str() command.

1 Like

Thanks it worked! Gracias my friend

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