Skip to content

merging command

2 messages · arun, Eliza Botto

#
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.