[R] histogram

arun smartpink111 at yahoo.com
Tue Mar 5 23:00:29 CET 2013


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



More information about the R-help mailing list