-1

I have a vector of hourly temperatures (DATA$TEMP) linked to dates (DATA$DATE) and thermometer position (DATA$PLACE).

I want identify the maximum temperature conditional on date and position. I can easily do this one date and position at a time, given I specify each date and position. eg.

x <- max(DATA$TEMP[DATA$DATE =="20/12/15" & DATA$PLACE=="room"])

However I have many dates and positions and would like a function that can run through each date / position combination and return a vector of max temps linked to each.

PereG
  • 1,796
  • 2
  • 22
  • 23
Shenzidog
  • 11
  • 1
  • 2

3 Answers3

1

Try this

library(dplyr)

x <- DATA %>% 
   group_by(DATE, PLACE) %>%                 
   summarise(maximum= max(TEMP)) 
PereG
  • 1,796
  • 2
  • 22
  • 23
0

Here an answer using base:

 by(DATA$TEMP, list(DATA$DATE, DATA$PLACE), max)

On a more general note, this kind of problem falls under the split-apply-combine paradigm. If you google this, you will find that there are quite many ways of doing this in R. From several base functions, to plyr, dplyr, and data.table versions. See e.g. here.

coffeinjunky
  • 11,254
  • 39
  • 57
0

Another option with data.table

library(data.table)
setDT(DATA)[, list(Max = max(TEMP)) , .(DATE, PLACE)]

Or with base R aggregate

aggregate(TEMP~DATE+PLACE, DATA, FUN= max)
akrun
  • 874,273
  • 37
  • 540
  • 662