How to make loops in R

Asked

Viewed 811 times

-2

I’m working on climate data and I need to do a boundary analysis for every element of my database. I need to make loops because my database is too big, but I don’t know where to start.

One 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 (has-temperature) is varying between 2 to 3 °C from one hour to the next. If this is within this limit, I need to assign a note, in which 0 means it is within the limit, 1 for variation outside this limit and 3 for no data (I have some lines without data). These notes can be either replacing the values in the same column or creating a new one.

  • 1

    Hi, Luana, hello, Luana! Your question is not clear, I suggest you describe what you are looking for in the limit analysis, why of the loop etc. I also suggest putting a small part of your base, 1 to 5 days with 4 to 6 columns, so that it can be clear and reproducible the results.

  • will it now become clearer? !!!

1 answer

4


There’s no reason to wear cycles for or others. R is a vector programming language, which means that it can process integer vectors at once. Here it goes.

First I will transform the column vector data in a class object Date. It will not be used but I believe that it is a step that can be useful in other situations and so it is okay to do it now.

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

Then create the column with the notes 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.

As a comment the following has now been requested.

On each line, assign the value 5 when tem_1min be among 0 and 40, 6 if you are outside this interval 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
  • You also know tell me which command I can use to create another column, but now I wanted to know if in each row of the tem_1min variable is within a range that goes from 0 to 40, and in the same way I previously wanted to assign notes.

  • @Luanacosta Is it again the variation from one line to the next line or is it the value in each line? And what are the notes that correspond to being within the range and not being?

  • It is the value in each row, assigning 5 when within the standard, 6 when outside the standard and 7 when there is no data, the default is 0 and 40. Ah! i tested the previous code and the note being assigned is only 1, even though it has grades 0 and 3.

  • @Luanacosta No resultado da resposta a linha 11 has nota equal to zero, is within the standard. None of the other differences are between 2 and 3.

  • @Luanacosta Feito, see the edition.

  • I adapted these codes to my database and I couldn’t get this 0 to appear, and I used the same codes as you and it didn’t happen either. I’ll see what I do here. Thank you very much @Rui Barradas, you helped me a lot.

  • Barred, the second part of the code is giving error, will you help me again. When I adapted to the other elements gives error: "not Meaningful for factors". AJUSTE REALIZADO: &#xA; &#xA; lim_temp <- (-40)<= dados_horas$temp_med & dados_horas$temp_med <= 60&#xA;dados_horas$nota1_temp_med [lim_temp] <- 5L&#xA;dados_horas$nota1_temp_med [!lim_temp] <- 6L datos_hours$nota1_temp_med <- 7L

  • @Luanacosta This means that temp_med is class factor, do dados_horas$temp_med <- as.numeric(as.character(dados_horas$temp_med)) before. AND dados_horas$nota1_temp_med <- 7L before the other two.

  • I have now tested and is showing the following error: "Nas introduced by coercion", right after the first command line: dates_hours$temp_med <- as.Numeric(as.Haracter(dates_hours$temp_med))..... lim_temp <- (-40)<= dates_hours$temp_med & datos_hours$temp_med <= 60... datos_hours$nota1_temp_med <- 7L.... datos_hours$nota1_temp_med [lim_temp] <- 0L... datos_hours$nota1_temp_med [!lim_temp] <- 3L... table(dates_hours$nota1_temp_med).... (... = new line in script)

Show 4 more comments

Browser other questions tagged

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