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,1 ,
146,3 ,
147,1 ,
148,1 ,
149,1 ,
150,2 ,
151,2 ,
152,2 ,
153,1 ,
154,1 ,
155,3 ,
156,1 ,
157,1 ,
158,1 ,
159,4 ,
160,1 ,
161,4 ,
162,1 ,
163,3 ,
164,3 ,
165,2 ,
166,4 ,
167,1 ,
168,1 ,
169,2 ,
170,3 ,
171,2 ,
172,1 ,
173,2 ,
174,1 ,
175,1 ,
176,1 ,
177,2 ,
178,1 ,
179,1 ,
180,1 ,
181,1 ,
182,1 ,
183,3 ,
184,1 ,
185,1 ,
186,2 ,
187,2 ,
188,2 ,
189,1 ,
190,1 ,
191,1 ,
192,1 ,
193,1 ,
194,3 ,
195,2 ,
196,3 ,
197,1 ,
198,1 ,
199,3 ,
200,2 ,
201,1 ,
202,4 ,
203,1 ,
204,1 ,
205,2 ,
206,1 ,
207,4 ,
208,3 ,
209,1 ,
210,3 ,
211,1 ,
212,3 ,
213,1 ,
214,1 ,
215,1 ,
216,2 ,
217,7 ,
218,1 ,
219,1 ,
220,1 ,
221,1 ,
222,1 ,
223,2 ,
224,1 ,
225,1 ,
226,1 ,
227,2 ,
228,1 ,
229,1 ,
230,1 ,
231,1 ,
232,1 ,
233,4 ,
234,1 ,
235,1 ,
236,1 ,
237,1 ,
238,1 ,
239,1 ,
240,1 ,
241,1 ,
242,1 ,
243,1 ,
244,2 ,
245,1 ,
246,1 ,
247,1 ,
248,1 ,
249,1 ,
250,1 ,
251,1 ,
252,1 ,
253,3 ,
254,1 ,
255,3 ,
256,2 ,
257,1 ,
258,1 ,
259,3 ,
260,1 ,
261,1 ,
262,1 ,
263,1 ,
264,1 ,
265,2 ,
266,1 ,
267,1 ,
268,4 ,
269,1 ,
270,2 ,
271,1 ,
272,1 ,
273,1 ,
274,1 ,
275,2 ,
276,2 ,
277,1 ,
278,5 ,
279,2 ,
280,1 ,
281,1 ,
282,1 ,
283,1 ,
284,1 ,
285,1 ,
286,1 ,
287,1 ,
288,5 ,
289,1 ,
290,2 ,
291,1 ,
292,1 ,
293,2 ,
294,1 ,
295,2 ,
296,1 ,
297,3 ,
298,1 ,
299,1 ,
300,2 ,
301,1 ,
302,2 ,
303,2 ,
304,6 ,
305,2 ,
306,1 ,
307,4 ,
308,2 ,
309,1 ,
310,3 ,
311,2 ,
312,1 ,
313,3 ,
314,1 ,
315,1 ,
316,1 ,
317,1 ,
318,1 ,
319,2 ,
320,6 ,
321,3 ,
322,2 ,
323,1 ,
324,5 ,
325,1 ,
326,1 ,
327,2 ,
328,1 ,
329,1 ,
330,1 ,
331,1 ,
332,1 ,
333,1 ,
334,4 ,
335,1 ,
336,1 ,
337,1 ,
338,1 ,
339,2 ,
340,2 ,
341,1 ,
342,1 ,
343,1 ,
344,5 ,
345,1 ,
346,3 ,
347,1 ,
348,1 ,
349,1 ,
350,2
),350,2, byrow=T)
I want to produce a histogram of these data. When I do this:
hist(m[,2])
It produces a histogram that has these counts:
$counts
[1] 224 67 0 34 0 14 0 8 0 2 0 1
It makes the breaks every 0.5, and I dont understand why. If I simply manually change the breaks to c(1:7), it doesn't produce the 0s anymore, but instead this:
$counts
[1] 291 34 14 8 2 1
It's an assignment for class, and we need to extract the frequency from the histogram. Our teacher gave us the outcome that we need to get, so I know that the frequencies need to be the same as in the first $count
index, (224, 67, 34...) but without the zeroes.
Why does the hist()
call produce these 0.5 breaks? The first frequencies seem to be the counts for 1 and 1.5, but that doesn't make sense because I only have whole numbers in my matrix.
Thanks in advance!
Histograms are meant for continuous random variables. They estimate the underlying density distribution. Instead, you see to have discrete values. R doesn't know this because they just look like numbers. So all the logic that goes into hist
choosing it's default breaks just assumes you have continuous observations. A more appropriate plot might be a bar plot
barplot(table(m[,2]))
Be careful when setting your own breaks, but default the lower value is excluded. So you would use one of
hist(m[,2], breaks=0:7)
hist(m[,2], breaks=1:7, include.lowest = TRUE, right = FALSE)
to get the same counts. But is really is just a result of the fact that a histogram is not the right tool to count discrete values. table(m[,2])
is a more direct way to get the counts.