[R-sig-Geo] Fwd: help with matrix

Sarah Goslee sarah.goslee at gmail.com
Fri Jul 19 15:40:30 CEST 2013


Hi,

Please don't cross-post: choose the most appropriate email list and
send your question only to that one. The best place for general
questions like this is R-help.

On Fri, Jul 19, 2013 at 9:12 AM, Kátia Emidio <kat.emidio at gmail.com> wrote:
>
> Hi,
>  I am a beginner at R and I would like to get information from a matrix,
> where the numbers are larger than or equal to 50m . This means that plants
> need to be apart 50m.  I  need to get the selection(the results) as a matrix
> or another object that allows me to identify which pair of species are 50m
> apart. Attached it is an example...

Attaching your data isn't nearly as useful as using dput() to include
it directly in the email. Many people dislike opening unsolicited
attachments, and the mail server strips most of them off anyway.

There are almost certainly more elegant ways to accomplish this.
Please move the discussion to R-help if you wish to continue it.

# recreate your data
adist <- structure(list(Nauc_calo_1 = c(0, 118.74, 172.27, 152.72, 119.45,
85.351), Nauc_calo_2 = c(118.74, 0, 165.8, 141.68, 67.186, 151.47
), Nauc_calo_3 = c(172.27, 165.8, 0, 24.331, 98.671, 104.72),
    Nauc_calo_4 = c(152.72, 141.68, 24.331, 0, 74.673, 93.503
    ), Nauc_calo_5 = c(119.45, 67.186, 98.671, 74.673, 0, 108.95
    ), Nauc_calo_6 = c(85.351, 151.47, 104.72, 93.503, 108.95,
    0)), .Names = c("Nauc_calo_1", "Nauc_calo_2", "Nauc_calo_3",
"Nauc_calo_4", "Nauc_calo_5", "Nauc_calo_6"), class = "data.frame",
row.names = c("Nauc_calo_1",
"Nauc_calo_2", "Nauc_calo_3", "Nauc_calo_4", "Nauc_calo_5", "Nauc_calo_6"
))

# identify which elements are >= 50
index50 <- data.frame(which(adist >= 50, arr.ind=TRUE))
# take unique pairs from your symmetric matrix
index50 <- subset(index50, row < col)
# get the associated names
results50 <- data.frame(row = rownames(adist)[index50$row], col =
colnames(adist)[index50$col], stringsAsFactors = FALSE)

Sarah

-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-sig-Geo mailing list