How to make loops in R

-2

I'm working with weather data and need to do a boundary analysis for each element of my database. I need to do loops because my database is too big, but I do not know where to start.

A part of my file is this:

dia hora    data    tem_1min    
1   1.0000  01/01/2012  20.463  
1   1.0417  01/01/2012  20.071  
1   1.0833  01/01/2012  19.462  
1   1.1250  01/01/2012  19.4    
1   1.1667  01/01/2012  18.528  
1   1.2083  01/01/2012  18.428  
1   1.2500  01/01/2012  18.411  
1   1.2917  01/01/2012  19.087  
1   1.3333  01/01/2012  20.063  
1   1.3750  01/01/2012  21.393  
1   1.4167  01/01/2012  23.722  
1   1.4583  01/01/2012  23.809  
1   1.5000  01/01/2012  24.49   
1   1.5417  01/01/2012  22.677  
1   1.5833  01/01/2012  22.905  
1   1.6250  01/01/2012  24.373  
1   1.6667  01/01/2012  25.07   
1   1.7083  01/01/2012  23.197  
1   1.7500  01/01/2012  22.631  
1   1.7917  01/01/2012  21.32   
1   1.8333  01/01/2012  20.503  
1   1.8750  01/01/2012  19.583  
1   1.9167  01/01/2012  19.911  
1   1.9583  01/01/2012  19.876  

I need to do the following analysis: 1) Tolerance analysis - if my element (tem- temperature) is varying from 2 to 3 ° C from one hour to another. If this is within this limit, I need to assign a note, where 0 means that it is within the limit, 1 for variation outside that limit and 3 for no data (I have some rows without data). These notes can be either replacing the values in the same column or creating a new one.

    
asked by anonymous 24.09.2018 / 01:37

2 answers

4

There is no reason to use for or other cycles. R is a vectorized programming language, which means it can process entire vectors at once. Here it goes.

First I'm going to transform the column data into an object of class Date . It will not be used but I think it is a step that can be useful in other situations and therefore it does not hurt to do so already.

dados$data <- as.Date(dados$data, "%d/%m/%Y")

Next, create the column with 0 , 1 or 3 .

dif <- c(0, diff(dados$tem_1min))
dados$nota <- as.integer(abs(dif) < 2 | abs(dif) > 3)
dados$nota[is.na(dados$nota)] <- 3L

head(dados, 10)
#   dia   hora       data tem_1min nota
#1    1 1.0000 2012-01-01   20.463    1
#2    1 1.0417 2012-01-01   20.071    1
#3    1 1.0833 2012-01-01   19.462    1
#4    1 1.1250 2012-01-01   19.400    1
#5    1 1.1667 2012-01-01   18.528    1
#6    1 1.2083 2012-01-01   18.428    1
#7    1 1.2500 2012-01-01   18.411    1
#8    1 1.2917 2012-01-01   19.087    1
#9    1 1.3333 2012-01-01   20.063    1
#10   1 1.3750 2012-01-01   21.393    1

Editing.

In comment the following has now been requested.

On each line, assign the value 5 when tem_1min is between 0 and 40 , 6 if outside this range and 7 if there is no data.

inx <- 0 <= dados$tem_1min & dados$tem_1min <= 40
dados$nota2 <- 7L
dados$nota2[inx] <- 5L
dados$nota2[!inx] <- 6L
    
25.09.2018 / 13:22
0

If I understood your question well, this should help.

For:

for(i in conjunto_de_valores){
  # comandos que 
  # serão repetidos
}

While:

while (condição){
     # comandos a serem repetidos
}

Repeat:

repeat {
    # Comandos a serem repetidos
    # if (Condição para que a repetição pare) break()
}
    
24.09.2018 / 07:02