Wrong result when averaging per group

Asked

Viewed 29 times

1

I am very beginner in R and am working on a large data.frame (approx 300000 data) that follows the example below:

                    Identification      Dates TEMP_Celcius
1   LONNIE POOL FIELD WEAVERVILLE  2014-01-01         13.0
2   LONNIE POOL FIELD WEAVERVILLE  2014-01-01         10.0
3   LONNIE POOL FIELD WEAVERVILLE  2014-01-01          8.0
4   LONNIE POOL FIELD WEAVERVILLE  2014-01-01          7.0
5   LONNIE POOL FIELD WEAVERVILLE  2014-01-01          5.0
6   LONNIE POOL FIELD WEAVERVILLE  2014-01-01          4.0
7   LONNIE POOL FIELD WEAVERVILLE  2014-01-01          3.0
8   LONNIE POOL FIELD WEAVERVILLE  2014-01-01          3.0
9   LONNIE POOL FIELD WEAVERVILLE  2014-01-01          2.0
10  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          2.0
11  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          2.0
12  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          2.0
13  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          0.0
14  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          0.0
15  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          0.0
16  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          0.0
17  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          0.0
18  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          0.0
19  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          0.0
20  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          0.0
21  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -1.0
22  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -1.0
23  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -2.0
24  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -1.0
25  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -2.0
26  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -2.0
27  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -2.0
28  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -2.0
29  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -2.0
30  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -2.0
31  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -2.0
32  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -2.0
33  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -3.0
34  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -3.0
35  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -3.0
36  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -3.0
37  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -3.0
38  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -3.0
39  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -3.0
40  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -3.0
41  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -4.0
42  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -4.0
43  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -3.0
44  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -4.0
45  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -4.0
46  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -4.0
47  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -4.0
48  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -4.0
49  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -4.0
50  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -3.0
51  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -3.0
52  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         -2.0
53  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          0.0
54  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          1.0
55  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          2.0
56  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          3.0
57  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          4.0
58  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          6.0
59  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          6.0
60  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          8.0
61  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          9.0
62  LONNIE POOL FIELD WEAVERVILLE  2014-01-01          9.0
63  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         10.0
64  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         11.0
65  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         12.0
66  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         12.0
67  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         12.0
68  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         12.0
69  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         13.0
70  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         13.0
71  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         13.0
72  LONNIE POOL FIELD WEAVERVILLE  2014-01-01         13.0
73  LONNIE POOL FIELD WEAVERVILLE  2014-01-02         12.0
74  LONNIE POOL FIELD WEAVERVILLE  2014-01-02         10.0
75  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          9.0
76  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          8.0
77  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          6.0
78  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          5.0
79  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          5.0
80  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          4.0
81  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          4.0
82  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          3.0
83  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          3.0
84  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          2.0
85  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          2.0
86  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          2.0
87  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          2.0
88  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          0.0
89  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          1.0
90  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          0.0
91  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          0.0
92  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          0.0
93  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          0.0
94  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          0.0
95  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          0.0
96  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          0.0
97  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          0.0
98  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          0.0
99  LONNIE POOL FIELD WEAVERVILLE  2014-01-02          0.0
100 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -1.0
101 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -1.0
102 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -1.0
103 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -2.0
104 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -2.0
105 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -1.0
106 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -2.0
107 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -2.0
108 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -2.0
109 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -2.0
110 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -2.0
111 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -2.0
112 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -2.0
113 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -2.0
114 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -3.0
115 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -3.0
116 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -3.0
117 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -3.0
118 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -3.0
119 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -3.0
120 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -3.0
121 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -3.0
122 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -2.0
123 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         -2.0
124 LONNIE POOL FIELD WEAVERVILLE  2014-01-02          0.0
125 LONNIE POOL FIELD WEAVERVILLE  2014-01-02          0.0
126 LONNIE POOL FIELD WEAVERVILLE  2014-01-02          1.0
127 LONNIE POOL FIELD WEAVERVILLE  2014-01-02          3.0
128 LONNIE POOL FIELD WEAVERVILLE  2014-01-02          4.0
129 LONNIE POOL FIELD WEAVERVILLE  2014-01-02          5.0
130 LONNIE POOL FIELD WEAVERVILLE  2014-01-02          6.0
131 LONNIE POOL FIELD WEAVERVILLE  2014-01-02          7.0
132 LONNIE POOL FIELD WEAVERVILLE  2014-01-02          8.0
133 LONNIE POOL FIELD WEAVERVILLE  2014-01-02          9.0
134 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         10.0
135 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         10.0
136 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         12.0
137 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         13.0
138 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         13.0
139 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         13.0
140 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         13.0
141 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         14.0
142 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         14.0
143 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         14.0
144 LONNIE POOL FIELD WEAVERVILLE  2014-01-02         14.0

I need to calculate the average temperature, max and min per day. Since I have several temperature results per day because they are given per hour. I used the following codes:

tapply (data$Temp_celcius, data$Dates, Mean)

tapply(Data$Temp_celcius, Data$Dates, Mean, na.rm = TRUE)

Aggregate(Data$Temp_celcius, by=list(TMEDIA=Data$Dates), Mean)

library(tidyverse) Data %>% group_by(Dates) %>% summarise(Mean = Mean(Temp_celcius), min = min(Temp_celcius), max= max(Temp_celcius))

But they all result in mistaken values. For the average of the first day for example (2014-01-01) the correct value would be 1.97 approx and the value that all codes result is 27.8. I don’t know what I’m doing wrong, can help me please?

NOTE: I have already checked the class of "Dates" and is "Date" and the temperature and is "Numeric".

Thank you so much already.

1 answer

0

I discovered my personal mistake. I had to have considered Identification in order for the average to be calculated correctly. Thanks in advance.

When I did:

Library(tidyverse) Data %>% group_by(Dates, Identification) %>% summarise(Mean = Mean(Temp_celcius), min = min(Temp_celcius), max= max(Temp_celcius))

worked out.

Browser other questions tagged

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