[R] merging command

arun smartpink111 at yahoo.com
Thu Jan 10 21:02:31 CET 2013


HI Eliza,

You could do this:
set.seed(15)
mat1<-matrix(sample(1:800,124*12,replace=TRUE),nrow=12) # smaller dataset
#Your codes
 list1<-list()
 for(i in 1:ncol(mat1)){
  list1[[i]]<-t(apply(mat1,1,function(x) x[i]-x))
  list1}
 x<-list1   
x<-matrix(unlist(x),nrow=12)
x<-abs(x)
 y<-colSums(x, na.rm=FALSE)
z<-matrix(y,ncol=10)
 z<-as.dist(z)
 z
 #    1   2   3   4   5   6   7   8   9
#2  319                                
#3  459 516                            
#4  385 504 260                        
#5  365 282 506 520                    
#6  318 363 373 305 383                
#7  382 277 459 457 363 370            
#8  526 521 431 443 523 472 608        
#9  329 534 358 374 382 393 467 429    
#10 364 377 393 365 419 420 346 472 489

#Modified code
z1<-as.dist(do.call(cbind,lapply(seq_len(ncol(mat1)),function(i) colSums(abs(t(apply(mat1,1, function(x) x[i]-x))),na.rm=FALSE))))
z1
#     1   2   3   4   5   6   7   8   9
#2  319                                
#3  459 516                            
#4  385 504 260                        
#5  365 282 506 520                    
#6  318 363 373 305 383                
#7  382 277 459 457 363 370            
#8  526 521 431 443 523 472 608        
#9  329 534 358 374 382 393 467 429    
#10 364 377 393 365 419 420 346 472 489

A.K.







________________________________
From: eliza botto <eliza_botto at hotmail.com>
To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com> 
Sent: Thursday, January 10, 2013 9:13 AM
Subject: merging command



Dear Arun,
i need you expertise to merge the following commands in to one step command.

mat1<-m
list1<-list()
for(i in 1:ncol(mat1)){
 list1[[i]]<-t(apply(mat1,1,function(x) x[i]-x))
 list1}
x<-list1  

x<-matrix(unlist(x),nrow=12)

x<-abs(x)

y<-colSums(x,
na.rm=FALSE)

z<-matrix(y,
ncol=124)

z<-as.dist(z)

i needed that distance to be executed in one command by merging all these commands.
is it possible??

thanks in advance

elisa                        




More information about the R-help mailing list