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
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. ________________________________ Fr istogram 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
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
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
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
HI Elisa, Just noticed the order of elements in vec1: You have to replace `vec1` dat2<- as.dist(dat1[,-1],upper=F,diag=F) vec1<- as.vector(dat2) ?head(vec1) #[1]? 5.796656 43.523023 38.193750 44.730182? 6.511703? 2.904954 #the order is based on columns #with dat1<- read.csv("rightest.csv",sep=",",header=TRUE,check.names=FALSE) label1=c("0-25","25-50","50-75") dat1New<- dat1[,-1] vec1<- unlist(lapply(seq_len(nrow(dat1New)),function(i) dat1New[i,][1:which(dat1New[i,]==0)-1])) ?head(vec1) #??????? 1???????? 1???????? 2???????? 1???????? 2???????? 3 # 5.796656 43.523023 36.305633 38.193750 31.623020? 5.391179? #correct order ?dat1[1:4,1:4] #? St.???????? 1???????? 2???????? 3 #1?? 1? 0.000000? 5.796656 43.523023 #2?? 2? 5.796656? 0.000000 36.305633 #3?? 3 43.523023 36.305633? 0.000000 #4?? 4 38.193750 31.623020? 5.391179 Name2<-unlist(lapply(0:123,function(i) if(length(rep(i+1,i)>=1)) paste("(",paste(rep(i+1,i)[1],seq_along(rep(i+1,i)),sep=","),")",sep="") else NULL)) dat3New<- data.frame(Name2,vec1) resNew<-t(aggregate(.~Name2,data=dat3New,function(x) table(cut(x,breaks=seq(0,75,25),labels=label1)))) colnames(resNew)<- resNew[1,] resNew1<- resNew[-1,] row.names(resNew1)<-gsub("vec1.","",row.names(resNew1)) Names3<-apply(resNew1,1,function(x) paste(names(which(x!=0)),collapse=",")) res2<- data.frame(Frequency=apply(resNew1,1,function(x) sum(1*(x!=0))), stations=Names3,stringsAsFactors=FALSE) A.K. ----- Original Message ----- From: arun <smartpink111 at yahoo.com> To: eliza botto <eliza_botto at hotmail.com> Cc: Sent: Tuesday, March 5, 2013 8:12 AM Subject: Re: histogram Dear Elisa, I already sent you the solution. ?Name2<-unlist(lapply(0:123,function(i) if(length(rep(i+1,i)>=1)) paste("(",paste(rep(i+1,i)[1],seq_along(rep(i+1,i)),sep=","),")",sep="") else NULL)) dat3New<- data.frame(Name2,vec1) resNew<-t(aggregate(.~Name2,data=dat3New,function(x) table(cut(x,breaks=seq(0,75,25),labels=label1)))) colnames(resNew)<- resNew[1,] resNew1<- resNew[-1,] row.names(resNew1)<-gsub("vec1.","",row.names(resNew1)) Names3<-apply(resNew1,1,function(x) paste(names(which(x!=0)),collapse=",")) res2<- data.frame(Frequency=apply(resNew1,1,function(x) sum(1*(x!=0))), stations=Names3,stringsAsFactors=FALSE) A.K. ________________________________ From: eliza botto <eliza_botto at hotmail.com> To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com> Sent: Tuesday, March 5, 2013 7:04 AM Subject: RE: histogram Dear Arun, Extremely sorry for replying you late. i really wanted to calculate the index of dat3. It alright for me, even if the size of output is really large. thanks in advance Elisa
Hi, I guess this is what you wanted. Attaching a plot from a subset (lstSub) dat1<- read.csv("rightest.csv",sep=",",header=TRUE,check.names=FALSE) label1=c("0-25","25-50","50-75") Name1<-unlist(lapply(0:123,function(i) rep(i+1,i))) dat1New<- dat1[,-1] vec1<- unlist(lapply(seq_len(nrow(dat1New)),function(i) dat1New[i,][1:which(dat1New[i,]==0)-1])) dat3<-data.frame(Name1,vec1) dat3$Categ<-as.character(cut(dat3$vec1,breaks=seq(0,75,25),labels=label1)) source("k.txt") lst1<- split(dat3,dat3$Name1) #lstSub<-split(dat3,dat3$Name1)[1:4] pdf("ElisaNew0_25.pdf") ?lapply(lst1,function(x) {indx025<-which(x$Categ=="0-25"); indx2550<-which(x$Categ=="25-50");indx5075<-which(x$Categ=="50-75"); if(length(indx025)>=1) {matplot(k[,indx025],ylim=c(0,5),type="l",col="grey",main=paste("range 0-25","line=",unique(x$Name1),sep=" "),xlab="T",ylab="Q"); lines(k[,unique(x$Name1)],type="l",col="black")} else NULL}) dev.off() pdf("ElisaNew25_50New.pdf") ?lapply(lst1,function(x) {indx025<-which(x$Categ=="0-25"); indx2550<-which(x$Categ=="25-50");indx5075<-which(x$Categ=="50-75"); if(length(indx2550)>=1) {matplot(k[,indx2550],ylim=c(0,5),type="l",col="grey",main=paste("range 25-50","line=",unique(x$Name1),sep=" "),xlab="T",ylab="Q"); lines(k[,unique(x$Name1)],type="l",col="black")} else NULL}) dev.off() pdf("ElisaNew50_75.pdf") ?lapply(lst1,function(x) {indx025<-which(x$Categ=="0-25"); indx2550<-which(x$Categ=="25-50");indx5075<-which(x$Categ=="50-75"); if(length(indx5075)>=1) {matplot(k[,indx5075],ylim=c(0,5),type="l",col="grey",main=paste("range 50-75","line=",unique(x$Name1),sep=" "),xlab="T",ylab="Q"); lines(k[,unique(x$Name1)],type="l",col="black")} else NULL}) dev.off() A.K. ________________________________ From: eliza botto <eliza_botto at hotmail.com> To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com> Sent: Tuesday, March 5, 2013 5:04 PM Subject: RE: histogram Dear Arun, Thanks for the update. Any success with the recently asked question? thanks elisa> Date: Tue, 5 Mar 2013 14:00:29 -0800 > From: smartpink111 at yahoo.com > Subject: Re: histogram > To: eliza_botto at hotmail.com > CC: r-help at r-project.org > > HI Elisa, > Just noticed the order of elements in vec1: > You have to replace `vec1` > dat2<- as.dist(dat1[,-1],upper=F,diag=F) > vec1<- as.vector(dat2) > ?head(vec1) > #[1]? 5.796656 43.523023 38.193750 44.730182? 6.511703? 2.904954 #the order is based on columns > > #with > dat1<- read.csv("rightest.csv",sep=",",header=TRUE,check.names=FALSE) > label1=c("0-25","25-50","50-75") > dat1New<- dat1[,-1] > vec1<- unlist(lapply(seq_len(nrow(dat1New)),function(i) dat1New[i,][1:which(dat1New[i,]==0)-1])) > ?head(vec1) > #??????? 1???????? 1???????? 2???????? 1???????? 2???????? 3 > # 5.796656 43.523023 36.305633 38.193750 31.623020? 5.391179? #correct order > ?dat1[1:4,1:4] > #? St.???????? 1???????? 2???????? 3 > #1?? 1? 0.000000? 5.796656 43.523023 > #2?? 2? 5.796656? 0.000000 36.305633 > #3?? 3 43.523023 36.305633? 0.000000 > #4?? 4 38.193750 31.623020? 5.391179 > > Name2<-unlist(lapply(0:123,function(i) if(length(rep(i+1,i)>=1)) paste("(",paste(rep(i+1,i)[1],seq_along(rep(i+1,i)),sep=","),")",sep="") else NULL)) > dat3New<- data.frame(Name2,vec1) > resNew<-t(aggregate(.~Name2,data=dat3New,function(x) table(cut(x,breaks=seq(0,75,25),labels=label1)))) > colnames(resNew)<- resNew[1,] > resNew1<- resNew[-1,] > row.names(resNew1)<-gsub("vec1.","",row.names(resNew1)) > Names3<-apply(resNew1,1,function(x) paste(names(which(x!=0)),collapse=",")) > res2<- data.frame(Frequency=apply(resNew1,1,function(x) sum(1*(x!=0))), stations=Names3,stringsAsFactors=FALSE) > > A.K. > > > > > > > ----- Original Message ----- > From: arun <smartpink111 at yahoo.com> > To: eliza botto <eliza_botto at hotmail.com> > Cc: > Sent: Tuesday, March 5, 2013 8:12 AM > Subject: Re: histogram > > Dear Elisa, > I already sent you the solution. > > > ?Name2<-unlist(lapply(0:123,function(i) > if(length(rep(i+1,i)>=1)) > paste("(",paste(rep(i+1,i)[1],seq_along(rep(i+1,i)),sep=","),")",sep="") > else NULL)) > dat3New<- data.frame(Name2,vec1) > resNew<-t(aggregate(.~Name2,data=dat3New,function(x) table(cut(x,breaks=seq(0,75,25),labels=label1)))) > colnames(resNew)<- resNew[1,] > resNew1<- resNew[-1,] > row.names(resNew1)<-gsub("vec1.","",row.names(resNew1)) > Names3<-apply(resNew1,1,function(x) paste(names(which(x!=0)),collapse=",")) > res2<- data.frame(Frequency=apply(resNew1,1,function(x) sum(1*(x!=0))), stations=Names3,stringsAsFactors=FALSE) > A.K. > > ________________________________ > From: eliza botto <eliza_botto at hotmail.com> > To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com> > Sent: Tuesday, March 5, 2013 7:04 AM > Subject: RE: histogram > > > > Dear Arun, > Extremely sorry for replying you late. i really wanted to calculate the index of dat3. It alright for me, even if the size of output is really large. > thanks in advance > > Elisa-------------- next part -------------- A non-text attachment was scrubbed... Name: ElisaNew25_50.pdf Type: application/pdf Size: 6647 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130305/48f88108/attachment.pdf>