-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Randomise pairwise elements of a matrix #24
Comments
Hi @SamPaplauskas -- I've recreated the problem with the code below, as you stated above
I think that the issue with the above is that R names the rows with the
Now you will see that some things get sorted into the upper triangle. This isn't a problem; you just need to check to see if something is in the upper triangle and swap the indices if so. This can be done with a couple for loops.
Verbally, what's going on with the for loop above is as follows. The code is going through each column (outer for loop), and within each column thorugh each row (inner for loop), thereby looking at each individual element of the I hope I've understood this correctly? |
Thanks so much - I will try this out and let you know if it does the trick... :) |
Here's a slightly different take - it just does the same as Brad's, but I think is different enough that it might be interesting. I just wrapped it into a function. The function
To use it
|
Title
Randomise pairwise elements of a matrix
Issue description
I have a matrix of values which are a table of differences.
I want to compare these obs values to a randomly generated null values.
This tests whether my obs values are significant, rather than occur by chance.
I want to keep the identity of the values...and randomise the pairwise comparisons which produced them.
What I have tried
sample function
but I am worried that this creates a non-sensical set of null values
Reproduce the problem
create matrix which shows pairwise differences, like my obs data matrix
m.obs <- matrix(round(runif(10), digits = 2), ncol=16, nrow=16)
m.obs[lower.tri(m.obs)] <- m.obs[upper.tri(m.obs)]
diag(m.obs) <- NA
this m is the same as
m.obs[upper.tri(m.obs)] <- NA
Desired outcome
x <- 1:16
m.null <- m.obs
x.perm <- sample(x, replace = FALSE)
rownames(m.null) <- x.perm
colnames(m.null) <- x.perm
m.null
I want this but where the values are then sorted by the randomised row and colnames
...and I did not accomplish this by already trying sort and order functions
The text was updated successfully, but these errors were encountered: