[R] converting manual command to loop command

Sarah Goslee sarah.goslee at gmail.com
Tue Dec 11 16:18:26 CET 2012


Given the same artificial data I provided, this will store all the
dist objects in a list:
fakedata.dlist <- lapply(1:ncol(fakedata),
function(x)dist(fakedata[,x], upper=TRUE, diag=TRUE))

Sarah


On Tue, Dec 11, 2012 at 10:15 AM, eliza botto <eliza_botto at hotmail.com> wrote:
> Dear Sarah,
> thankyou very much it worked...
> what if i want traditional distance matrices instead of distance column
> vectors??
> i hope i'm not bothering you...
> thanks in advance
> eliza
>
>
>> Date: Tue, 11 Dec 2012 09:57:37 -0500
>> Subject: Re: [R] converting manual command to loop command
>> From: sarah.goslee at gmail.com
>> To: eliza_botto at hotmail.com
>> CC: r-help at r-project.org
>>
>> 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