-1

i now have a data frame which the month and day are in different columns. How should i write the code to get exactly the last day of each month?

The data is like:
Month    Day
1        1
1        2
1        3
2        1
2        5

Thanks!!

Matt Lin
  • 33
  • 2

2 Answers2

2

We can use aggregate

aggregate(Day ~Month, df1, FUN = max)

If the 'Month', 'Day' are already ordered

df1[!duplicated(df1$Month, fromLast = TRUE),]
#   Month Day
#3     1   3
#5     2   5

A tidyverse approach would be

library(tidyverse)
df1 %>%
    group_by(Month) %>%
    slice(n())  # assumes the 'Day' is ordered

If the 'Day' is not ordered, get the max

df1 %>%
    group_by(Month) %>%
    summarise(Day = max(Day))
akrun
  • 874,273
  • 37
  • 540
  • 662
1
library(sqldf)
sqldf("select max(Day),Month from df1 group by Month")

or

library(Hmisc)
summarize(df1$Day,df1$Month,max)
Ajay Ohri
  • 3,382
  • 3
  • 30
  • 60