-1

I am new to R.I would like to create a frequency table and normal frequency distribution curve of the following data. What is the best way for doing this?

x1= 5,9,5,12,35,55,43,35,18,12,9,22,22,55,7,12
x2= 8,12,24,16,15,20,20,24,35,5,7,9,6,8,6,18,14,12,16,12,5,6,9,10,10,12,18,15,16  

your suggestions would be appreciated!

  • @andrie I think, you misunderstood my question. I don't need a barplot.I need a distribution curve. – user3997009 Sep 01 '14 at 12:09
  • OK, I reopened your question to give you the opportunity to edit and clarify. In particular, show how your question is different from all any other question about `[r] frequency distribution`. Explain what you mean by a distribution curve and how that is different from a barplot. – Andrie Sep 01 '14 at 12:25
  • There are many sites for density plots but not as many for frequency tables. One good site is: http://ww2.coastal.edu/kingw/statistics/R-tutorials/descriptive.html – rnso Sep 01 '14 at 15:01

2 Answers2

1

Try:

x1= c(5, 9, 5, 12, 35, 55, 43, 35, 18, 12, 9, 22, 22, 55, 7, 12)                       
x2 = c(8, 12, 24, 16, 15, 20, 20, 24, 35, 5, 7, 9, 6, 8, 6, 18, 14, 12, 16, 12, 5, 6, 9, 10, 10, 12, 18, 15, 16)

library(ggplot2)
ggplot()+geom_density(aes(x2),color='red')+geom_density(aes(x1), color='blue')

enter image description here

(hist(x2))                 # this is the command; rest is output
$breaks                                                                                                                 
[1]  5 10 15 20 25 30 35                                                                                                

$counts                                                                                                                 
[1] 12  7  7  2  0  1                                                                                                   

$density
[1] 0.082758621 0.048275862 0.048275862 0.013793103 0.000000000 0.006896552

$mids
[1]  7.5 12.5 17.5 22.5 27.5 32.5

$xname
[1] "x2"

$equidist
[1] TRUE

attr(,"class")
[1] "histogram"

For a tabular version:

hh = hist(x2)
bb = hh$breaks

hh.breaks = as.character()
for(i in 2:length(bb)) {
    hh.breaks[i-1]=paste0(bb[i-1],'-',bb[i])
}
dd = data.frame(hh.breaks, hh$counts, hh$density, hh$mids)

dd

 hh.breaks hh.counts  hh.density hh.mids
1      5-10        12 0.082758621     7.5
2     10-15         7 0.048275862    12.5
3     15-20         7 0.048275862    17.5
4     20-25         2 0.013793103    22.5
5     25-30         0 0.000000000    27.5
6     30-35         1 0.006896552    32.5

enter image description here

You may find this link useful: http://www.statmethods.net/graphs/density.html There are many others if you simply search.

rnso
  • 23,686
  • 25
  • 112
  • 234
0
x <- sample(1:10,30,TRUE)
x
 [1] 10  9  2  5  4  9  2 10  2  2  4  5  9  2 10  8  8  6  7  3  8  1  1  7  6  8  6  2  3  5
table(x)
x
 1  2  3  4  5  6  7  8  9 10 
 2  6  2  2  3  3  2  4  3  3 

plot(table(x),type = "l")

enter image description here

bartektartanus
  • 15,284
  • 6
  • 74
  • 102