[R] addition in the initial question

arun smartpink111 at yahoo.com
Sun Feb 17 22:23:20 CET 2013


Dear Elisa,
Try this:
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)
vec2<-vec1[1:26]
names(vec2)<-LETTERS[1:26]
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="")))
 dat1<-data.frame(vec2,class=cut(vec2,breaks=seq(0,50,5),labels=label1),stringsAsFactors=FALSE)
 vecNew<-cut(vec2,breaks=seq(0,50,5),labels=label1)
res<-data.frame(aggregate(row.names(dat1)~class,dat1,function(x) x),Frequency=as.data.frame(table(vecNew))[,2],stringsAsFactors=FALSE)
 names(res)[2]<- "header_elements_class"
 res
#      class header_elements_class Frequency
#1    0<x<=5               P, R, X         3
#2   5<x<=10                  F, G         4
#3  10<x<=15            C, M, S, Z         3
#4  15<x<=20               B, N, T         2
#5  20<x<=25                  H, J         2
#6  25<x<=30                  E, L         3
#7  30<x<=35               A, V, Y         3
#8  35<x<=40               I, K, W         2
#9  40<x<=45                  O, Q         2
#10 45<x<=50                  D, U         2
 str(res)
#'data.frame':    10 obs. of  3 variables:
# $ class                : Factor w/ 10 levels "0<x<=5","5<x<=10",..: 1 2 3 4 5 6 7 8 9 10
# $ header_elements_class:List of 10
#  ..$ 0: chr  "P" "R" "X"
#  ..$ 1: chr  "F" "G"
#  ..$ 2: chr  "C" "M" "S" "Z"
#  ..$ 3: chr  "B" "N" "T"
#  ..$ 4: chr  "H" "J"
#  ..$ 5: chr  "E" "L"
#  ..$ 6: chr  "A" "V" "Y"
#  ..$ 7: chr  "I" "K" "W"
#  ..$ 8: chr  "O" "Q"
#  ..$ 9: chr  "D" "U"
# $ Frequency            : int  3 4 3 2 2 3 3 2 2 2


A.K.





________________________________
From: eliza botto <eliza_botto at hotmail.com>
To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com> 
Sent: Sunday, February 17, 2013 3:27 PM
Subject: addition in the initial question



Dear Arun,

just a small change in the initial question.
what if, instead of counting number i want to get the headers of the numbers falling in each range. finally counting those numbers to get frequencies and then drawing histograms and fitting density curve.

[HEADER] A   B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W   X Y  Z
[VECTOR] 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 


classheader of elements in each class Frequency

0<x<=5P, R, X       3
5<x<=10F, G       2
10<x<=15  C, S       2
.....
...
...
...
45<x<=50       U       1

thankyou very much in advance

elisa



More information about the R-help mailing list