[R] Odp: Histogram problem
Petr PIKAL
petr.pikal at precheza.cz
Thu Aug 20 16:24:18 CEST 2009
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