3
Good afternoon, everyone!
I’m starting in R (starting well) and tried to make a script to solve a simple operation that, however, was performed entirely in excel during a mini-course I did, giving a great job.
the purpose of the script is to read the data from a table containing a history series of precipitation data (1974-2018) and calculate the standard deviation only for the recitals events within a range (50mm up to 300mm).
I was able to write the script and arrive at the expected result, however, I would like to know how to use a loop command to store the expected interval’s pricing data without having to have 30 lines for it.
Below follows the elaborate script:
## importação dos dados
teste <- read.table("serie.txt", h=T, sep=";")
## apresentação de parte dos dados
teste [1:12, 1:10]
## definição do limite mínimo de precipitação
def.min <- 50
## definição do limite máximo de precipitação
def.max <- 300
## agrupamento dos episódios com precipitação entre 50 mm e 300 mm para cada dia
filtro.dia1 <- teste[teste$Dia1 >= def.min & teste$Dia1 < def.max ,]
filtro.dia2 <- teste[teste$Dia2 >= def.min & teste$Dia2< def.max ,]
filtro.dia3 <- teste[teste$Dia3 >= def.min & teste$Dia3< def.max ,]
filtro.dia4 <- teste[teste$Dia4 >= def.min & teste$Dia4< def.max ,]
filtro.dia5 <- teste[teste$Dia5 >= def.min & teste$Dia5< def.max ,]
filtro.dia6 <- teste[teste$Dia6 >= def.min & teste$Dia6< def.max ,]
filtro.dia7 <- teste[teste$Dia7 >= def.min & teste$Dia7< def.max ,]
filtro.dia8 <- teste[teste$Dia8 >= def.min & teste$Dia8< def.max ,]
filtro.dia9 <- teste[teste$Dia9 >= def.min & teste$Dia9< def.max ,]
filtro.dia10 <- teste[teste$Dia10 >= def.min & teste$Dia10< def.max ,]
filtro.dia11 <- teste[teste$Dia11 >= def.min & teste$Dia11< def.max ,]
filtro.dia12 <- teste[teste$Dia12 >= def.min & teste$Dia12< def.max ,]
filtro.dia13 <- teste[teste$Dia13 >= def.min & teste$Dia13< def.max ,]
filtro.dia14 <- teste[teste$Dia14 >= def.min & teste$Dia14< def.max ,]
filtro.dia15 <- teste[teste$Dia15 >= def.min & teste$Dia15< def.max ,]
filtro.dia16 <- teste[teste$Dia16 >= def.min & teste$Dia16< def.max ,]
filtro.dia17 <- teste[teste$Dia17 >= def.min & teste$Dia17< def.max ,]
filtro.dia18 <- teste[teste$Dia18 >= def.min & teste$Dia18< def.max ,]
filtro.dia19 <- teste[teste$Dia19 >= def.min & teste$Dia19< def.max ,]
filtro.dia20 <- teste[teste$Dia20 >= def.min & teste$Dia20< def.max ,]
filtro.dia21 <- teste[teste$Dia21 >= def.min & teste$Dia21< def.max ,]
filtro.dia22 <- teste[teste$Dia22 >= def.min & teste$Dia22< def.max ,]
filtro.dia23 <- teste[teste$Dia23 >= def.min & teste$Dia23< def.max ,]
filtro.dia24 <- teste[teste$Dia24 >= def.min & teste$Dia24< def.max ,]
filtro.dia25 <- teste[teste$Dia25 >= def.min & teste$Dia25< def.max ,]
filtro.dia26 <- teste[teste$Dia26 >= def.min & teste$Dia26< def.max ,]
filtro.dia27 <- teste[teste$Dia27 >= def.min & teste$Dia27< def.max ,]
filtro.dia28 <- teste[teste$Dia28 >= def.min & teste$Dia28< def.max ,]
filtro.dia29 <- teste[teste$Dia29 >= def.min & teste$Dia29< def.max ,]
filtro.dia30 <- teste[teste$Dia30 >= def.min & teste$Dia30 < def.max,]
filtro.dia31 <- teste[teste$Dia31 >= def.min & teste$Dia31 < def.max ,]
## apresentação de todos os episódios com precipitação entre 50 mm e 300 mm
c(filtro.dia1$Dia1 , filtro.dia2$Dia2 , filtro.dia3$Dia3 , filtro.dia4$Dia4 , filtro.dia5$Dia5 , filtro.dia6$Dia6 , filtro.dia7$Dia7 , filtro.dia8$Dia8 , filtro.dia9$Dia9 , filtro.dia10$Dia10 , filtro.dia11$Dia11 , filtro.dia12$Dia12 , filtro.dia13$Dia13 , filtro.dia14$Dia14 , filtro.dia15$Dia15 , filtro.dia16$Dia16 , filtro.dia17$Dia17 , filtro.dia18$Dia18 , filtro.dia19$Dia19 , filtro.dia20$Dia20 , filtro.dia21$Dia21 , filtro.dia22$Dia22 , filtro.dia23$Dia23 , filtro.dia24$Dia24 , filtro.dia25$Dia25 , filtro.dia26$Dia26 , filtro.dia27$Dia27 , filtro.dia28$Dia28 , filtro.dia29$Dia29 , filtro.dia30$Dia30, filtro.dia31$Dia31)
## desvio padrão para os valores de precipitação entre 50 mm e 300 mm
dp.dias <- sd(c(filtro.dia1$Dia1 , filtro.dia2$Dia2 , filtro.dia3$Dia3 , filtro.dia4$Dia4 , filtro.dia5$Dia5 , filtro.dia6$Dia6 , filtro.dia7$Dia7 , filtro.dia8$Dia8 , filtro.dia9$Dia9 , filtro.dia10$Dia10 , filtro.dia11$Dia11 , filtro.dia12$Dia12 , filtro.dia13$Dia13 , filtro.dia14$Dia14 , filtro.dia15$Dia15 , filtro.dia16$Dia16 , filtro.dia17$Dia17 , filtro.dia18$Dia18 , filtro.dia19$Dia19 , filtro.dia20$Dia20 , filtro.dia21$Dia21 , filtro.dia22$Dia22 , filtro.dia23$Dia23 , filtro.dia24$Dia24 , filtro.dia25$Dia25 , filtro.dia26$Dia26 , filtro.dia27$Dia27 , filtro.dia28$Dia28 ,filtro.dia29$Dia29 , filtro.dia30$Dia30 , filtro.dia31$Dia31))
dp.dias
I also made an attempt to loop using while, but it didn’t work, returning the message "Error: Object 'filter.dia' not found". The script is as follows:
teste <- read.table("serie.txt", h=T, sep=";")
teste [1:12, 1:10]
def.min <- 50
def.max <- 300
while (i <= 31) {
filtro.dia[i] <- teste[teste$Dia[i] >= def.min & teste$Dia[i] < def.max ,]
filtro.dia[i]$Dia[i]
i <- i + 1
}
Here is also an example of the table being used:
> teste [1:100, 1:12]
Municipios Postos Latitude Longitude Anos Meses Total Dia1 Dia2 Dia3 Dia4 Dia5
1 Sobral SOBRAL -3.660639 -40.34489 1974 1 151.5 0.0 0 0 0 0
2 Sobral SOBRAL -3.660639 -40.34489 1974 2 167.0 21.0 0 15 0 0
3 Sobral SOBRAL -3.660639 -40.34489 1974 3 336.0 0.0 0 0 5 0
4 Sobral SOBRAL -3.660639 -40.34489 1974 4 244.0 5.5 7 0 0 0
5 Sobral SOBRAL -3.660639 -40.34489 1974 5 447.5 0.0 0 15 16 0
6 Sobral SOBRAL -3.660639 -40.34489 1974 6 82.0 25.0 0 0 15 0
7 Sobral SOBRAL -3.660639 -40.34489 1974 7 0.0 0.0 0 0 0 0
8 Sobral SOBRAL -3.660639 -40.34489 1974 8 0.0 0.0 0 0 0 0
9 Sobral SOBRAL -3.660639 -40.34489 1974 9 0.0 0.0 0 0 0 0
10 Sobral SOBRAL -3.660639 -40.34489 1974 10 0.0 0.0 0 0 0 0
11 Sobral SOBRAL -3.660639 -40.34489 1974 11 0.0 0.0 0 0 0 0
12 Sobral SOBRAL -3.660639 -40.34489 1974 12 15.0 0.0 0 0 0 0
13 Sobral SOBRAL -3.660639 -40.34489 1975 1 39.0 0.0 0 0 0 0
14 Sobral SOBRAL -3.660639 -40.34489 1975 2 247.0 0.0 24 0 16 0
15 Sobral SOBRAL -3.660639 -40.34489 1975 3 367.6 10.0 11 0 33 65
16 Sobral SOBRAL -3.660639 -40.34489 1975 4 157.0 0.0 0 0 0 10
17 Sobral SOBRAL -3.660639 -40.34489 1975 5 219.0 12.0 13 16 0 0
18 Sobral SOBRAL -3.660639 -40.34489 1975 6 23.5 4.0 0 0 5 0
19 Sobral SOBRAL -3.660639 -40.34489 1975 7 0.0 0.0 0 0 0 0
20 Sobral SOBRAL -3.660639 -40.34489 1975 8 0.0 0.0 0 0 0 0
21 Sobral SOBRAL -3.660639 -40.34489 1975 9 0.0 0.0 0 0 0 0
22 Sobral SOBRAL -3.660639 -40.34489 1975 10 0.0 0.0 0 0 0 0
23 Sobral SOBRAL -3.660639 -40.34489 1975 11 0.0 0.0 0 0 0 0
24 Sobral SOBRAL -3.660639 -40.34489 1975 12 55.0 0.0 0 0 6 49
25 Sobral SOBRAL -3.660639 -40.34489 1976 1 16.0 0.0 0 0 0 0
26 Sobral SOBRAL -3.660639 -40.34489 1976 2 151.0 0.0 0 0 10 3
27 Sobral SOBRAL -3.660639 -40.34489 1976 3 242.5 0.0 0 0 5 0
28 Sobral SOBRAL -3.660639 -40.34489 1976 4 195.0 37.0 37 0 0 1
29 Sobral SOBRAL -3.660639 -40.34489 1976 5 35.0 0.0 0 0 0 25
30 Sobral SOBRAL -3.660639 -40.34489 1976 6 0.0 0.0 0 0 0 0
31 Sobral SOBRAL -3.660639 -40.34489 1976 7 0.0 0.0 0 0 0 0
32 Sobral SOBRAL -3.660639 -40.34489 1976 8 0.0 0.0 0 0 0 0
33 Sobral SOBRAL -3.660639 -40.34489 1976 9 0.0 0.0 0 0 0 0
34 Sobral SOBRAL -3.660639 -40.34489 1976 10 0.0 0.0 0 0 0 0
35 Sobral SOBRAL -3.660639 -40.34489 1976 11 0.0 0.0 0 0 0 0
36 Sobral SOBRAL -3.660639 -40.34489 1976 12 0.0 0.0 0 0 0 0
37 Sobral SOBRAL -3.660639 -40.34489 1977 1 166.0 0.0 0 0 0 27
38 Sobral SOBRAL -3.660639 -40.34489 1977 2 184.0 50.0 13 0 12 0
39 Sobral SOBRAL -3.660639 -40.34489 1977 3 169.0 0.0 10 0 8 0
40 Sobral SOBRAL -3.660639 -40.34489 1977 4 111.0 7.0 0 15 0 0
41 Sobral SOBRAL -3.660639 -40.34489 1977 5 129.0 0.0 11 26 0 44
42 Sobral SOBRAL -3.660639 -40.34489 1977 6 73.0 0.0 0 0 0 0
43 Sobral SOBRAL -3.660639 -40.34489 1977 7 12.0 0.0 0 0 0 0
44 Sobral SOBRAL -3.660639 -40.34489 1977 8 0.0 0.0 0 0 0 0
45 Sobral SOBRAL -3.660639 -40.34489 1977 9 0.0 0.0 0 0 0 0
46 Sobral SOBRAL -3.660639 -40.34489 1977 10 0.0 0.0 0 0 0 0
47 Sobral SOBRAL -3.660639 -40.34489 1977 11 0.0 0.0 0 0 0 0
48 Sobral SOBRAL -3.660639 -40.34489 1977 12 0.0 0.0 0 0 0 0
49 Sobral SOBRAL -3.660639 -40.34489 1978 1 66.0 0.0 0 0 0 0
50 Sobral SOBRAL -3.660639 -40.34489 1978 2 132.0 0.0 0 0 0 0
51 Sobral SOBRAL -3.660639 -40.34489 1978 3 90.0 0.0 0 0 0 4
52 Sobral SOBRAL -3.660639 -40.34489 1978 4 160.0 0.0 28 0 0 0
53 Sobral SOBRAL -3.660639 -40.34489 1978 5 208.0 67.0 18 6 5 0
54 Sobral SOBRAL -3.660639 -40.34489 1978 6 3.0 0.0 0 0 0 0
55 Sobral SOBRAL -3.660639 -40.34489 1978 7 22.0 0.0 0 0 0 0
56 Sobral SOBRAL -3.660639 -40.34489 1978 8 0.0 0.0 0 0 0 0
57 Sobral SOBRAL -3.660639 -40.34489 1978 9 0.0 0.0 0 0 0 0
58 Sobral SOBRAL -3.660639 -40.34489 1978 10 0.0 0.0 0 0 0 0
59 Sobral SOBRAL -3.660639 -40.34489 1978 11 0.0 0.0 0 0 0 0
60 Sobral SOBRAL -3.660639 -40.34489 1978 12 5.0 0.0 0 0 0 2
61 Sobral SOBRAL -3.660639 -40.34489 1979 1 68.0 0.0 0 0 0 0
62 Sobral SOBRAL -3.660639 -40.34489 1979 2 80.0 0.0 0 0 0 0
63 Sobral SOBRAL -3.660639 -40.34489 1979 3 173.0 32.0 0 37 0 0
64 Sobral SOBRAL -3.660639 -40.34489 1979 4 180.0 0.0 0 0 4 0
65 Sobral SOBRAL -3.660639 -40.34489 1979 5 100.0 0.0 0 0 0 0
66 Sobral SOBRAL -3.660639 -40.34489 1979 6 7.0 0.0 0 0 0 0
67 Sobral SOBRAL -3.660639 -40.34489 1979 7 0.0 0.0 0 0 0 0
68 Sobral SOBRAL -3.660639 -40.34489 1979 8 0.0 0.0 0 0 0 0
69 Sobral SOBRAL -3.660639 -40.34489 1979 9 0.0 0.0 0 0 0 0
70 Sobral SOBRAL -3.660639 -40.34489 1979 10 0.0 0.0 0 0 0 0
71 Sobral SOBRAL -3.660639 -40.34489 1979 11 0.0 0.0 0 0 0 0
72 Sobral SOBRAL -3.660639 -40.34489 1979 12 9.9 0.0 0 0 0 0
73 Sobral SOBRAL -3.660639 -40.34489 1980 1 48.0 0.0 0 0 5 0
74 Sobral SOBRAL -3.660639 -40.34489 1980 2 291.0 0.0 0 0 0 0
75 Sobral SOBRAL -3.660639 -40.34489 1980 3 160.0 0.0 0 4 0 20
76 Sobral SOBRAL -3.660639 -40.34489 1980 4 60.0 0.0 0 0 0 8
77 Sobral SOBRAL -3.660639 -40.34489 1980 5 14.0 0.0 0 7 4 0
78 Sobral SOBRAL -3.660639 -40.34489 1980 6 0.0 0.0 0 0 0 0
79 Sobral SOBRAL -3.660639 -40.34489 1980 7 0.0 0.0 0 0 0 0
80 Sobral SOBRAL -3.660639 -40.34489 1980 8 0.0 0.0 0 0 0 0
81 Sobral SOBRAL -3.660639 -40.34489 1980 9 0.0 0.0 0 0 0 0
82 Sobral SOBRAL -3.660639 -40.34489 1980 10 0.0 0.0 0 0 0 0
83 Sobral SOBRAL -3.660639 -40.34489 1980 11 0.0 0.0 0 0 0 0
From now on, thank you for your attention!
Want to group by
Municipios
,Postos
,Ano
,Mês
, or nothing? Only the standard deviation for allDia*
in the range considered?– Rui Barradas
In principle, just calculate the standard deviation for all days of the table that are within the range considered.
– Lincoln Medeiros
Done, see if this is it.
– Rui Barradas