[R] converting manual command to loop command

Sarah Goslee sarah.goslee at gmail.com
Tue Dec 11 15:57:37 CET 2012


Does this solve your problem:

fakedata <- matrix(runif(100), ncol=10)
fakedata.dist <- apply(fakedata, 2, function(x)as.vector(dist(x,
upper=TRUE, diag=TRUE)))


The columns of the resulting matrix contain the distance vectors.

Sarah

On Tue, Dec 11, 2012 at 9:45 AM, eliza botto <eliza_botto at hotmail.com> wrote:
>
> Dear useRs,
> i have certain commands for some operations in R. They are good if you have a small dataset but my dataset, apart from what i used in the recent past, is prety large. I want to convert these massive sets of commands into a simple loop.
> Your help is required on it
> thanks in advance
> eliza
> kindly note:
> "e" is matrix whose each column has to be executed into a distance vector
>
> ##To get a distance matrix
>
>>a<-dist(e[,1], upper=TRUE, diag=TRUE)
>
> ##To convert it in a matrix
>
>>a<-matrix(a, ncol=1)
>
>>b<-dist(e[,2], upper=TRUE, diag=TRUE)
>
>>b<-matrix(b, ncol=1)
>
>>c<-dist(e[,3], upper=TRUE, diag=TRUE)
>
>>c<-matrix(c, ncol=1)
>
>>d<-dist(e[,4], upper=TRUE, diag=TRUE)
>
>>d<-matrix(d, ncol=1)
>
>>ee<-dist(e[,5], upper=TRUE, diag=TRUE)
>
>>ee<-matrix(ee, ncol=1)
>
>>f<-dist(e[, 6], upper=TRUE, diag=TRUE)
>
>>f<-matrix(f, ncol=1)
>
>>g<-dist(e[, 7], upper=TRUE, diag=TRUE)
>
>>g<-matrix(g, ncol=1)
>
>>h<-dist(e[, 8], upper=TRUE, diag=TRUE)
>
>>h<-matrix(h, ncol=1)
>
>
--
Sarah Goslee
http://www.functionaldiversity.org




More information about the R-help mailing list