[R] histogram

arun smartpink111 at yahoo.com
Mon Mar 4 21:37:06 CET 2013


Hi,
Sorry, I forgot names(which(....)
Names2<-apply(res1,1,function(x) paste(names(which(x!=0)),collapse=","))

res2<- data.frame(Frequency=apply(res1,1,function(x) sum(1*(x!=0))), stations=Names2,stringsAsFactors=FALSE)
 res2
 #     Frequency
#0-25        121
#25-50       122
#50-75        76
                                                                                                                                                                                                                                                                                                                                                                                             #stations
#0-25    #2,4,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124
#25-50 #3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124
#50-75                                                                                                                                      #11,17,23,26,28,31,32,34,35,36,37,38,39,40,41,42,48,49,51,54,57,59,60,62,65,66,69,70,71,74,76,77,78,79,80,81,82,83,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,122,124


A.K.



----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: eliza botto <eliza_botto at hotmail.com>
Cc: R help <r-help at r-project.org>
Sent: Monday, March 4, 2013 3:26 PM
Subject: Re: histogram

Hi,

dat1<- read.csv("rightest.csv",sep=",",header=TRUE,check.names=FALSE)
 dat2<- as.dist(dat1[,-1],upper=F,diag=F)
vec1<- as.vector(dat2)
label1=c("0-25","25-50","50-75")
Name1<-unlist(lapply(0:123,function(i) rep(i+1,i)))
dat3<-data.frame(Name1,vec1)
res<-t(aggregate(.~Name1,data=dat3,function(x) table(cut(x,breaks=seq(0,75,25),labels=label1))))
colnames(res)<- res[1,]
res1<- res[-1,]
row.names(res1)<-gsub("vec1.","",row.names(res1))
res1
Names2<-apply(res1,1,function(x) paste(which(x!=0),collapse=","))
res2<- data.frame(Frequency=apply(res1,1,function(x) sum(1*(x!=0))), stations=Names2,stringsAsFactors=FALSE)

res2
#      Frequency
#0-25        121
#25-50       122
#50-75        76
                                                                                                                                                                                                                                                                                                                                                                                           #stations
#0-25    #1,3,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123
#25-50 #2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123
#50-75                                                                                                                                     #10,16,22,25,27,30,31,33,34,35,36,37,38,39,40,41,47,48,50,53,56,58,59,61,64,65,68,69,70,73,75,76,77,78,79,80,81,82,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,121,123

A.K.

________________________________
From: eliza botto <eliza_botto at hotmail.com>
To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com> 
Sent: Monday, March 4, 2013 3:21 PM
Subject: RE: histogram



Dear Arun,


Thanks for replying....
Although codes well defined my problem but the table in the end should look like the following

its just an imaginary table.....
Range                          stations                                         Frequency
0-25                           1,2,3,8,9                                           5
25-50                          4,10,11,100                                         4
50-75                          55,56,57                                            3
Where the "station" column shows the stations where distance of station is between the corresponding range.... like 1,2,3,8,9 have the distance between 0-25

i hope you wont mind

elisa




> Date: Mon, 4 Mar 2013 11:56:43 -0800
> From: smartpink111 at yahoo.com
> Subject: Re: histogram
> To: eliza_botto at hotmail.com
> CC: r-help at r-project.org
> 
> Hi Elisa,
> 
> I am not sure about the output you wanted.
> dat1<- read.csv("rightest.csv",sep=",",header=TRUE,check.names=FALSE)
>  dat2<- as.dist(dat1[,-1],upper=F,diag=F)
> vec1<- as.vector(dat2)
> label1=c("0-25","25-50","50-75")
> Count1<- as.data.frame(table(cut(vec1,breaks=seq(0,75,25),labels=label1))) #Overall count
>  Count1
> #   Var1 Freq
> #1  0-25 5465
> #2 25-50 1992
> #3 50-75  169
> 
> 
> Name1<-unlist(lapply(0:123,function(i) rep(i+1,i)))
>  length(Name1)
> #[1] 7626
> dat3<-data.frame(Name1,vec1)
> res<-t(aggregate(.~Name1,data=dat3,function(x) table(cut(x,breaks=seq(0,75,25),labels=label1))))
> colnames(res)<- res[1,]
>  res1<- res[-1,]
> row.names(res1)<-gsub("vec1.","",row.names(res1))
> res1
> #      2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
> #0-25  1 0 2 0 2 3 2 1  1  1  3  1  1  3  2  3  6  3  5  2  4  8 13 21 21 23 20
> #25-50 0 2 1 4 3 3 5 7  8  8  8 11 12 11 13 12 11 15 14 18 17 12 10  3  2  3  6
> #50-75 0 0 0 0 0 0 0 0  0  1  0  0  0  0  0  1  0  0  0  0  0  2  0  0  2  0  1
> -----------------------------------------------------------------------------------------------------
> 
> A.K.
> 
> 
> 
> 
> 
> ________________________________
> From: eliza botto <eliza_botto at hotmail.com>
> To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com> 
> Sent: Monday, March 4, 2013 11:36 AM
> Subject: histogram
> 
> 
> 
> Dear Arun,
> 
> i have a distance matrix as attached in excel file with this email. You can read the data via R and 
> after reading the data i want you to extract the lower part of distance matrix by 
> as.dist(x, upper=F, diag=F). You will see that there 
> are 124 stations in my study. After that, i want to divide the data into three intervals 0-25, 25-75, 
> 75-100. Then i want to count the number of stations falling in each interval, which will be called 
> "Frequency". After that i want to draw the following table
> Range                                                         stations                                                                 Frequency
> 0-25                                                   names of station                                                      Number of stations
> 25-50      
> 50-75
> Finally, i want to draw histogram. i know i asked same kind of question before, but those commands are not working on distance matrix.
> 
> thankyou very very much in advance
> elisa




More information about the R-help mailing list