[R] histogram
arun
smartpink111 at yahoo.com
Tue Mar 5 01:14:36 CET 2013
Hi,
If you meant to get the array index from `res1`, then this will do it. But, if it is from 'dat3`, it will be huge as each index is unique.
indx<-which(apply(res1,1,function(x) x!=0) ,arr.ind=TRUE)
Names2<-do.call(rbind,lapply(1:3,function(i) {x<-indx[indx[,2]==i,]; paste(apply(x,1,function(y) paste("(",paste(y,collapse=","),")",sep="")),collapse=",")} ))
res2<- data.frame(Frequency=apply(res1,1,function(x) sum(1*(x!=0))), stations=Names2,stringsAsFactors=FALSE)
A.K.
________________________________
From: eliza botto <eliza_botto at hotmail.com>
To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com>
Sent: Monday, March 4, 2013 5:50 PM
Subject: RE: histogram
Dear Arun,
Just a small inquiry i have.
you can see that in the results, there are some stations which are repeating themselves like station number 16 which is included in all three ranges. its because for station 16 there are over 100 values. So to get rid of it................ what if instead of stations, i want to locate the coordinates of each station in the final table. like for coordinate (17row,16col), it should be in range 0-25 and (18row,17col) should be included in range 25-50.
so my final table should look like
Range stations Frequency
0-25 (1,4),(2,3),(8,9) 3
25-50 (4,10),(11,100) 2
50-75 (55,56),(57,60) 2
is it possible?
thanks alot...
elisa
> Date: Mon, 4 Mar 2013 12:38:22 -0800
> From: smartpink111 at yahoo.com
> Subject: Re: histogram
> To: eliza_botto at hotmail.com
>
> Sometimes, you make mistake when you are quick. I forgot names(which(..)). THe corrected version is sent.
> Thanks.
> Arun
>
>
>
>
>
>
> ________________________________
> From: eliza botto <eliza_botto at hotmail.com>
> To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com>
> Sent: Monday, March 4, 2013 3:31 PM
> Subject: RE: histogram
>
>
>
> My GOD, you are so quick.
> Thankyou so very much indeed...
> stay blessed.
>
> elisa
>
>
> > Date: Mon, 4 Mar 2013 12:26:44 -0800
> > From: smartpink111 at yahoo.com
> > Subject: Re: histogram
> > To: eliza_botto at hotmail.com
> > CC: r-help at r-project.org
> >
> > 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