[R] Histogram problem

Petr PIKAL petr.pikal at precheza.cz
Fri Aug 21 08:31:27 CEST 2009


Hi

R does what you said to it it shall do! 

Without  **reproducible** code and explanation what you did, what you get 
and what you **expect** to get it would be quite complicated task to 
understand what your code actually do.

e.g. this

> > for (i in 1:Count){
> >
> >     for (j in 1:Count2){
> >
> >
> >         if ((SPXdf[i,2] < Bins[j]))
> >         {Num[j] = Num[j] + 1}
> >     }
> >
> > }

add 1 to Num[j] each time when SPXdf[i,2] < Bins[j] but without real 
numbers and understanding why you can not use hist and are making your own 
histogram function it is hard to say what do you want. 
I am pretty sure you are inventing a wheel.

Regards
Petr

Conrad Addo <conrad.addo at gmail.com> napsal dne 20.08.2009 16:29:36:

> Petr
> 
> Actually I wanted to find why the NUm vector was adding incrementally, 
not 
> about the CSV.
> 
> Thanks

> On Thu, Aug 20, 2009 at 3:24 PM, Petr PIKAL <petr.pikal at precheza.cz> 
wrote:
> Hi
> 
> r-help-bounces at r-project.org napsal dne 20.08.2009 15:33:38:
> 
> > I'm trying to create a histogram from the following code, but my data
> keeps
> > adding the Num vector and plotting that.  My data in the CSV file is
> just
> > one vector.  Does anyone know why?

> Well, I think that only you know why CSV file is just one vector, (which 
I
> do not believe as SPXdf[,2] does not coplain).
> 
> >
> > Thanks
> > Conrad
> >
> >
> > SPXdf = data.frame(read.csv(file = "SPX.csv", header = TRUE, sep = 
",",
> dec
> > = ".", fill = TRUE))
> 
> 
> > SPXdf = data.frame(read.csv(file = "SPX.csv", header = TRUE, sep = 
",",
> dec
> + = ".", fill = TRUE))
> Error in file(file, "r") : cannot open the connection
> In addition: Warning message:
> In file(file, "r") : cannot open file 'SPX.csv': No such file or 
directory
> > Max = max(SPXdf[,2])
> Error: object 'SPXdf' not found
> > Min = min(SPXdf[,2])
> E
> 
> I can not retrieve the data, it throws an error.
> 
> >
> > Max = max(SPXdf[,2])
> > Min = min(SPXdf[,2])
> > Count = length(SPXdf[,2])
> > sig = sd(SPXdf[,2])
> >
> > BinSize = (3.5*sig)/(Count^(1/3))
> > BinNum = (Max - Min)/(BinSize)
> > Bins = seq(Min,Max,by = BinSize)
> > Count2 = length(Bins)
> > Num = mat.or.vec(1,Count2)
> >
> >
> > for (i in 1:Count){
> >
> >     for (j in 1:Count2){
> >
> >
> >         if ((SPXdf[i,2] < Bins[j]))
> >         {Num[j] = Num[j] + 1}
> >     }
> >
> > }
> >
> > barplot(Num,Bins, xlab = "SPXdf", ylab = "Frequency")

> R does what you require. You want barplot of Num vector with width
> according to Bin vector.
> 
> Just as a curiosity, does hist(SPXdf[,2]) show a histogram?
> 
> Regards
> Petr
> 
> 
> >
> >    [[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.




More information about the R-help mailing list