[R] histogram

arun smartpink111 at yahoo.com
Sun Feb 17 21:49:08 CET 2013


HI Elisa,
You could use ?cut()


vec1<-c(33,18,13,47,30,10,6,21,39,25,40,29,14,16,44,1,41,4,15,20,46,32,38,5,31,12,48,27,36,24,34,2,35,11,42,9,8,7,26,22,43,17,19,28,23,3,49,37,50,45)
label1<-unlist(lapply(mapply(c,lapply(seq(0,45,5),function(x) x),lapply(seq(5,50,5),function(x) x),SIMPLIFY=FALSE),function(i) paste(i[1],"<x<=",i[2],sep="")))

 Count1<-as.data.frame(table(cut(vec1,breaks=seq(0,50,5),labels=label1)))


 Count1
#       Var1 Freq
#1    0<x<=5    5
#2   5<x<=10    5
#3  10<x<=15    5
#4  15<x<=20    5
#5  20<x<=25    5
#6  25<x<=30    5
#7  30<x<=35    5
#8  35<x<=40    5
#9  40<x<=45    5
#10 45<x<=50    5
hist(vec1,breaks=seq(0,50,5),freq=T)

 hist(vec1,breaks=seq(0,50,5),prob=TRUE)
 lines(density(vec1))

label2<-unlist(lapply(mapply(c,lapply(seq(0,45,5),function(x) x),lapply(seq(55,by=50,length.out=10),function(x) x),SIMPLIFY=FALSE),function(i) paste(i[1],"<x<=",i[2],sep="")))
 count2<-as.data.frame(table(cut(vec1,breaks=c(0,55,100,145,190,235,280,325,370,415,460),labels=label2)))
count2
#        Var1 Freq
#1    0<x<=55   50
#2   5<x<=105    0
#3  10<x<=155    0
#4  15<x<=205    0
#5  20<x<=255    0
#6  25<x<=305    0
#7  30<x<=355    0
#8  35<x<=405    0
#9  40<x<=455    0
#10 45<x<=505    0

hist(vec1,breaks=c(0,55,100,145,190,235,280,325,370,415,460))
 hist(vec1,breaks=c(0,55,100,145,190,235,280,325,370,415,460),prob=TRUE)
 lines(density(vec1))

A.K.
________________________________
From: eliza botto <eliza_botto at hotmail.com>
To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com> 
Sent: Sunday, February 17, 2013 2:35 PM
Subject: histogram



Dear Arun,
[text file is attached in case format of email is changed]
For the following data set
33 18 13 47 30 10  6 21 39 25 40 29 14 16 44  1 41  4 15 20 46 32 38  5 31 12 48 27 36 24 34  2 35 11 42  9  8  7 26 22 43 17 19 28 23  3 49 37 50 45

1. i first of all want to make classes in the following form

class

0<x<=5
5<x<=10
10<x<=15
15<x<=20
.....
...
...
...
45<x<=50

and then i want to count the number of elements in each class and ultimately i want to execute a table in the following form

classnumber of elements in each class

0<x<=55
5<x<=105
10<x<=155
15<x<=205
.....
...
...
...
45<x<=505

the command which i used is to count the number of elements in each class was

> length(which(x > 45 & x <= 50))

2. is there a loop command which can count the number of elements in each range instead of using an individual command for each range?? 
3. then i want to make histogram out of that table.
4. lastly, i want to fit density curve on those histograms.

I am greatful to you in advance.

elisa



More information about the R-help mailing list