[R] Histogram

David Winsemius dwinsemius at comcast.net
Mon Jul 30 19:48:09 CEST 2012


On Jul 30, 2012, at 9:29 AM, Jim Silverton wrote:

> I have the following codes:
> Now, suppose I have x = runif(1000,0,1, rep(1, 250), rep(0, 100) and  
> I want
> to create a 'bin' for the 0's and the 1's and put the rest of the  
> values in
> say about 20 bins. How can this be done?

?cut

-- 
David.
>
> Jim
>
>
>
>
>
> On Thu, Jul 5, 2012 at 4:08 PM, Sarah Goslee  
> <sarah.goslee at gmail.com> wrote:
>
>> Which gives Jim two ways to arrive at exactly the same result, just
>> different means of specifying the probs for quantile().
>>
>> Sarah
>>
>> On Thu, Jul 5, 2012 at 4:01 PM, Rui Barradas <ruipbarradas at sapo.pt>  
>> wrote:
>>> Hello,
>>>
>>> With the confusion between bin size and width the OP started, I'll
>> repost my
>>> answer with a final line. Sorry for the repetition.
>>>
>>>
>>> h <- hist(x, breaks=quantile(x, probs=seq(0, 1, by=1/20)))
>>> h$counts
>>> [1] 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50
>>>
>>>
>>> Hope this helps,
>>>
>>> Rui Barradas
>>>
>>> Em 05-07-2012 20:47, Sarah Goslee escreveu:
>>>>
>>>> There's no reason you can't do that with normally-distributed data,
>>>> though I'm not sure why you'd want to. My point was rather that you
>>>> can't specify the bin width and size both. If you let the bin size
>>>> vary, this will work:
>>>>
>>>> set.seed(1234)
>>>> mydata <- rnorm(1000, mean = 2, sd = 4)
>>>> mydata.hist <- hist(mydata, breaks=quantile(mydata, probs=seq(0, 1,
>>>> length.out = length(mydata)/50 + 1)))
>>>> mydata.hist$counts
>>>>
>>>>
>>>> Sarah
>>>>
>>>> On Thu, Jul 5, 2012 at 3:37 PM, Jim Silverton <jim.silverton at gmail.com 
>>>> >
>>>> wrote:
>>>>>
>>>>> Thanks Sarah!!
>>>>> Ok so if I have say x = runif(1000,0,1) say instead if the  
>>>>> normal and I
>>>>> want
>>>>> a histogram with bins that have an equal number of observations.  
>>>>> For
>>>>> example
>>>>> if I want each bin to have 50 observations, how do I do this?
>>>>>
>>>>>
>>>>>
>>>>> On Thu, Jul 5, 2012 at 3:34 PM, Sarah Goslee <sarah.goslee at gmail.com 
>>>>> >
>>>>> wrote:
>>>>>>
>>>>>>
>>>>>> Hi Jim,
>>>>>>
>>>>>> You can't specify both number of bins and bin size. You can  
>>>>>> specify
>>>>>> breaks: either the number of bins or the location of  
>>>>>> breakpoints. A
>>>>>> histogram with 20 bins of 50 observations each must by  
>>>>>> definition come
>>>>>> from a uniform distribution.
>>>>>>
>>>>>> What are you trying to accomplish?
>>>>>>
>>>>>> Sarah
>>>>>>
>>>>>> On Thu, Jul 5, 2012 at 3:29 PM, Jim Silverton <
>> jim.silverton at gmail.com>
>>>>>> wrote:
>>>>>>>
>>>>>>> I have a column of 1000 datapoints from the normal  
>>>>>>> distribution with
>>>>>>> mean 2
>>>>>>> and variance 4. How can I get a histogram of these  
>>>>>>> observations with
>> 20
>>>>>>> bins with each bin having 50 observations?
>>>>>>>
>>>>>>> --
>>>>>>> Thanks,
>>>>>>> Jim.
>>>>>>
>>>>>>
>>>>
>>>
>>
>
>
>
> -- 
> Thanks,
> Jim.
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
Alameda, CA, USA



More information about the R-help mailing list