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)
You don't say what your intent is, but for most applications it's important to preserve the pairwise matches. Here's one way to do that. library(ecodist) # for the convenient functions lower() and full() library(ade4) x1 <- c(rep(0,4),1) x2 <- c(rep(0,2),rep(1,3)) x3 <- c(rep(1,3), rep(0,2)) X <- as.data.frame(rbind(x1, x2, x3)) jc.dist <- dist.binary(X, method=1) jc.full <- full(jc.dist) dim(jc.full) # 3 x 3 randsample <- sample(1:nrow(jc.full)) jc.randfull <- jc.full[randsample, randsample] jc.randdist <- lower(jc.randfull) # if it needs to be of class dist attributes(jc.randdist) <- attributes(jc.dist) If just putting the distances in a random order is really all right for your application, you can skip everything except the last step. jc.new <- sample(jc.dist) attributes(jc.new) <- attributes(jc.dist) Sarah On Wed, Apr 15, 2009 at 10:02 AM, Dale Steele <dale.w.steele at gmail.com> 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) >-- Sarah Goslee http://www.functionaldiversity.org
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.
Seemingly Similar Threads
- Any ideas for the most efficient algoritm to reshuffle tiles based on this description.
- Conditional CCA and Monte Carlo - Help!
- Problemas instalando el paquete "ade4"
- Correspondence analysis with R -follow up
- AMOVA results from ade4 different than in the reference publication