Average temperature in R

Asked

Viewed 226 times

1

Have:

     Data Sala.1 Sala.2 Sala.3 Horas
1  02/08/2013   20.5   19.7   21.6 15:00
2  02/08/2013   19.7   18.9   20.2 15:30
3  02/08/2013   19.7   19.1   20.0 16:00
4  02/08/2013   19.7   18.8   19.8 16:30
5  02/08/2013   19.8   18.8   19.9 17:00
6  02/08/2013   19.9   19.2   20.0 17:30
7  02/08/2013   19.9   18.9   20.0 18:00
8  02/08/2013   20.0   19.1   20.1 18:30
9  02/08/2013   20.2   19.4   20.3 19:00
10 02/08/2013   20.3   19.1   20.4 19:30
11 02/08/2013   20.5   19.6   20.6 20:00
12 02/08/2013   20.5   19.2   20.6 20:30
13 02/08/2013   20.8   19.8   20.9 21:00
14 02/08/2013   20.7   19.3   20.8 21:30
15 02/08/2013   21.0   19.9   21.1 22:00
16 02/08/2013   20.9   19.5   21.1 22:30
17 02/08/2013   20.9   19.3   21.1 23:00
18 02/08/2013   21.2   20.1   21.4 23:30

I need to calculate the average temperature per room during the time of stay (7:00 till 23:30) Does anyone know how I do in R?

3 answers

2

You can use data.table. In addition to facilitating group and subset calculations, it has functions to handle date and time.

I duplicated your data to contain more than a day:

dados <- read.table(text = c('
    Data       Sala.1 Sala.2 Sala.3 Horas
    02/08/2013   20.5   19.7   21.6 15:00
    02/08/2013   19.7   18.9   20.2 15:30
    02/08/2013   19.7   19.1   20.0 16:00
    02/08/2013   19.7   18.8   19.8 16:30
    02/08/2013   19.8   18.8   19.9 17:00
    02/08/2013   19.9   19.2   20.0 17:30
    02/08/2013   19.9   18.9   20.0 18:00
    02/08/2013   20.0   19.1   20.1 18:30
    02/08/2013   20.2   19.4   20.3 19:00
    02/08/2013   20.3   19.1   20.4 19:30
    02/08/2013   20.5   19.6   20.6 20:00
    02/08/2013   20.5   19.2   20.6 20:30
    02/08/2013   20.8   19.8   20.9 21:00
    02/08/2013   20.7   19.3   20.8 21:30
    02/08/2013   21.0   19.9   21.1 22:00
    02/08/2013   20.9   19.5   21.1 22:30
    02/08/2013   20.9   19.3   21.1 23:00
    02/08/2013   21.2   20.1   21.4 23:30
    03/08/2013   20.5   19.7   21.6 15:00
    03/08/2013   19.7   18.9   20.2 15:30
    03/08/2013   19.7   19.1   20.0 16:00
    03/08/2013   19.7   18.8   19.8 16:30
    03/08/2013   19.8   18.8   19.9 17:00
    03/08/2013   19.9   19.2   20.0 17:30
    03/08/2013   19.9   18.9   20.0 18:00
    03/08/2013   20.0   19.1   20.1 18:30
    03/08/2013   20.2   19.4   20.3 19:00
    03/08/2013   20.3   19.1   20.4 19:30
    03/08/2013   20.5   19.6   20.6 20:00
    03/08/2013   20.5   19.2   20.6 20:30
    03/08/2013   20.8   19.8   20.9 21:00
    03/08/2013   20.7   19.3   20.8 21:30
    03/08/2013   21.0   19.9   21.1 22:00
    03/08/2013   20.9   19.5   21.1 22:30
    03/08/2013   20.9   19.3   21.1 23:00
    03/08/2013   21.2   20.1   21.4 23:30'),
    header = TRUE)

library(data.table)

dt <- as.data.table(dados)

dt$Horas <- as.ITime(dt$Horas, format = '%H:%M')

medias <- dt[ Horas >= as.ITime('7:00:00') & Horas <= as.ITime('23:30:00'), lapply(.SD, mean), by = Data ][ , Horas := NULL ]

>     medias
         Data   Sala.1   Sala.2 Sala.3
1: 02/08/2013 20.34444 19.31667  20.55
2: 03/08/2013 20.34444 19.31667  20.55

1

Next, follow the code snippet that solves your problem.

tempo_inicial <- '19:00'
tempo_final <- '23:30'

#Encontrar indice da hora inicial e final
#match retorna o primeiro casamento que encontrar na coluna de horas
indices <- match(c(tempo_inicial, tempo_final), dados[, 5])

#primeiro indice da hora inicial, segundo possui a hora final.
#considerando que o horario esta ordenado em ordem crescente.
inicio <- indices[1]
fim <- indices[2]

#delimita o dataframe nesse intervalo, alem de delimitar as
#colunas 2, 3 e 4 que correspondem a posição das salas.
dados.temp <- dados[inicio:fim, c(2, 3, 4)]

#por fim, calcular suas medias
colMeans(dados.temp)

0

I modified some of your data:

# vetores de dados
Data <- rep('02/08/2013', 13) # cria 13 datas iguais
Sala.1 <- c(20.5,19.7,19.7,19.7,19.8,19.9,19.9,20.0,20.2,20.3,20.5,20.5,20.8)
Sala.2 <-c(20.5,19,19.7,19.7,19.9,19.9,19.9,20.0,20.2,20.3,20.5,20.5,20.3)
Horas <- c('15:00', '15:30', '16:00', '16:30', '17:00', '17:30', '18:00', 
                   '18:30', '19:00', '19:30', '20:00', '20:30','21:00')

# criando o data frame
dados <- data.frame(Data, Sala.1, Sala.2, Horas)



dados

    Data Sala.1 Sala.2 Horas
1  02/08/2013   20.5   20.5 15:00
2  02/08/2013   19.7   19.0 15:30
3  02/08/2013   19.7   19.7 16:00
4  02/08/2013   19.7   19.7 16:30
5  02/08/2013   19.8   19.9 17:00
6  02/08/2013   19.9   19.9 17:30
7  02/08/2013   19.9   19.9 18:00
8  02/08/2013   20.0   20.0 18:30
9  02/08/2013   20.2   20.2 19:00
10 02/08/2013   20.3   20.3 19:30
11 02/08/2013   20.5   20.5 20:00
12 02/08/2013   20.5   20.5 20:30
13 02/08/2013   20.8   20.3 21:00

Calculate the average temperature between 16h and 18h30 for room 1. We see in the above data the data lines of room 1 at this time correspond to line 3 to 8 of the dataframe. So:

# funcao temp media
m.temp <- function(intervalo){
  T <- c()                     # vetor vazio
  for(i in intervalo){         # itera da linha inicial da hora ate a final
    T<- c(T,dados$Sala.1[i])   # adciona valores das T das do intervalo
  }
mean(T)                        # retorna a media das T
}

m.temp(3:8)     # media entre 16h e 18h30 da Sala.1 
19.83333 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.