[R] How to Reshuffle a distance object

Gavin Simpson gavin.simpson at ucl.ac.uk
Thu Apr 16 09:45:57 CEST 2009


Dale Steele wrote:
> I would like to randomly shuffle a distance object, such as the one
> created by ade4{dist.binary} below. My first attempt, using
> sample(jc.dist) creates a shuffled vector, losing the lower triangular
> structure of the distance object.  How can I Ishuffle the lower
> triangular part of a distance matrix without losing the structure?
> Thanks.  --Dale
> 
> x1 <- c(rep(0,4),1)
> x2 <- c(rep(0,2),rep(1,3))
> x3 <- c(rep(1,3), rep(0,2))
> X <- rbind(x1,x2,x3)
> X
> X <- as.data.frame(X)
> library(ade4)
> jc.dist  <- dist.binary(X, method=1)
> sample(jc.dist)

Convert to the full matrix, shuffle that and then convert back to a dist 
object.

jc.mat <- as.matrix(jc.dist)
shuff <- sample(nrow(jc.mat))
jc.shuff <- as.dist(jc.mat[shuff, shuff])

HTH

Gavin

> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list