Reputation:
What is sql group by equivalent of R For example I want perform this on a data.frame
SELECT MAX(temp) from table group by day
by subset function
Note::
I know about sqldf and I don't want use it for some purpose
Upvotes: 0
Views: 101
Reputation: 887901
We can either use aggregate
from base R
. Here, I am using the formula method, where the grouping variable is on the RHS of the ~
. Specify the FUN
as max
to get the maximum value of 'temp'
aggregate(temp~day, df1, FUN = max)
Or using dplyr
, we group by 'day' and get the max
of temp within summarise
.
library(dplyr)
df1 %>%
group_by(day) %>%
summarise(temp= max(temp))
Or using data.table
, we convert the 'data.frame' to 'data.table' (setDT(df1)
), grouped by 'day', we get the max
of 'temp'.
library(data.table)
setDT(df1)[, list(temp=max(temp)), by = day]
Upvotes: 1