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!!
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))
library(sqldf)
sqldf("select max(Day),Month from df1 group by Month")
or
library(Hmisc)
summarize(df1$Day,df1$Month,max)