[R] manipulate a matrix2
jim holtman
jholtman at gmail.com
Mon Jul 21 18:10:28 CEST 2008
Will this do it for you:
> ?print.default
> x
[,1] [,2] [,3] [,4] [,5]
[1,] 1 6 11 16 21
[2,] 2 7 12 17 22
[3,] 3 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25
> x[upper.tri(x)] <- NA
> x
[,1] [,2] [,3] [,4] [,5]
[1,] 1 NA NA NA NA
[2,] 2 7 NA NA NA
[3,] 3 8 13 NA NA
[4,] 4 9 14 19 NA
[5,] 5 10 15 20 25
> print(x, na.print='.')
[,1] [,2] [,3] [,4] [,5]
[1,] 1 . . . .
[2,] 2 7 . . .
[3,] 3 8 13 . .
[4,] 4 9 14 19 .
[5,] 5 10 15 20 25
>
On Mon, Jul 21, 2008 at 11:47 AM, Jon Hak <Jon_Hak at natureserve.org> wrote:
> The vegan matrix produces values of similarity between sample sites.
> Because the matrix uses the same samples for the row names and the
> column header it has duplicates on either side of the base diagonal
> (below).
>
> 3 7 8 11 12
> 3 0 0.6 1 0.3 0.85
> 7 0.66 0 1 0.65 0.95
> 8 1 1 0 1 1
> 11 0.3 0.65 1 0 0.9
> 12 0.85 0.95 1 0.9 0
>
> Ideally, the matrix should look like;
>
> 3 7 8 11 12
> 3 0
> 7 0.66 0
> 8 1 1 0
> 11 0.3 0.65 1 0
> 12 0.85 0.95 1 0.9 0
>
> This is probably a question for the Vegan developers, but I really
> appreciate your (and the lists) insight.
>
> -----Original Message-----
> From: jim holtman [mailto:jholtman at gmail.com]
> Sent: Monday, July 21, 2008 9:18 AM
> To: Jon Hak
> Cc: r-help at r-project.org
> Subject: Re: [R] manipulate a matrix2
>
> I am not familiar with the vegdist function. What defines a duplicate
> in the matrix? There are ways if identifying if more than one row
> meets the criteria duplicates and then removing them. Can you give an
> illustration of what you mean with a before/after data representation.
>
> On Mon, Jul 21, 2008 at 10:22 AM, Jon Hak <Jon_Hak at natureserve.org>
> wrote:
>> Thanks Jim, that was exactly what I was after.
>>
>> On a second note, do you have any insight into pulling out the
>> duplicates in this type of matrix?
>> I thought that was what the upper=FALSE is in:
>> csv.dis <- vegdist(csv.m, method='jaccard', binary=FALSE, diag=FALSE,
>> upper=FALSE). I just need either the lower or upper portion, with the
>> zeros (,3 & ,3) being the dividing line.
>>
>> [,3] [,5] [,6] [,9] [,11]
>>
>> [3,] 0 2 3 4 5
>>
>> [5,] 2 0 8 9 10
>>
>> [6,] 3 8 0 14 15
>>
>> [9,] 4 9 14 0 20
>>
>> [11,] 5 10 15 20 0
>>
>> Thanks again,
>>
>> Jon
>>
>>
>>
>> -----Original Message-----
>> From: jim holtman [mailto:jholtman at gmail.com]
>> Sent: Friday, July 18, 2008 9:56 AM
>> To: Jon Hak
>> Cc: r-help at r-project.org
>> Subject: Re: [R] manipulate a matrix2
>>
>> Is this what you want:
>>
>>> x
>> [,3] [,5] [,6] [,9] [,11]
>> [,3] 1 6 11 16 21
>> [,5] 2 7 12 17 22
>> [,6] 3 8 13 18 23
>> [,9] 4 9 14 19 24
>> [,11] 5 10 15 20 25
>>> library(reshape)
>>> melt(x)
>> X1 X2 value
>> 1 [,3] [,3] 1
>> 2 [,5] [,3] 2
>> 3 [,6] [,3] 3
>> 4 [,9] [,3] 4
>> 5 [,11] [,3] 5
>> 6 [,3] [,5] 6
>> 7 [,5] [,5] 7
>> 8 [,6] [,5] 8
>> 9 [,9] [,5] 9
>> 10 [,11] [,5] 10
>> 11 [,3] [,6] 11
>> 12 [,5] [,6] 12
>> 13 [,6] [,6] 13
>> 14 [,9] [,6] 14
>> 15 [,11] [,6] 15
>> 16 [,3] [,9] 16
>> 17 [,5] [,9] 17
>> 18 [,6] [,9] 18
>> 19 [,9] [,9] 19
>> 20 [,11] [,9] 20
>> 21 [,3] [,11] 21
>> 22 [,5] [,11] 22
>> 23 [,6] [,11] 23
>> 24 [,9] [,11] 24
>> 25 [,11] [,11] 25
>>>
>>
>>
>> On Fri, Jul 18, 2008 at 11:10 AM, Jon Hak <Jon_Hak at natureserve.org>
>> wrote:
>>> Building upon Jim's answer below (Thanks Jim, that helped a lot), I
>> need
>>> to pickup where this thread left off. I'm using Vegan to calculate
>> the
>>> Jaccard's Index and the Row.Names and column names are represented in
>> my
>>> matrix as seen here.
>>>
>>> [,3] [,5] [,6] [,9] [,11]
>>>
>>> [3,] 0 6 11 16 21
>>>
>>> [5,] 2 0 12 17 22
>>>
>>> [6,] 3 8 0 18 23
>>>
>>> [9,] 4 9 14 0 24
>>>
>>> [11,] 5 10 15 20 0
>>>
>>>
>>>
>>> When I use the command;
>>> xy <- cbind(row=as.vector(row.names(x)), col=as.vector(colnames(x)),
>>> value=as.vector(x))
>>>
>>> I get the list (the column value is the issue);
>>>
>>> row col value
>>> [1,] 3 1 0
>>> [2,] 5 1 2
>>> [3,] 6 1 3
>>> [4,] 9 1 4
>>> [5,] 11 1 5
>>> [6,] 3 2 6
>>> [7,] 5 2 0
>>> [8,] 6 2 8
>>> [9,] 9 2 9
>>> [10,] 11 2 10
>>> [11,] 3 3 11
>>> [12,] 5 3 0
>>>
>>> I would really like the col value to equal the actual name, not the
>>> column number. What am I missing? The analysis is very large, 6k
> x6k
>>> matrix so automating the process is a high priority.
>>>
>>> Thanks,
>>> Jon
>>>
>>>
>>>
>>> From: jim holtman <jholtman_at_gmail.com
>>>
>>
> <mailto:jholtman_at_gmail.com?Subject=Re:%20%5BR%5D%20manipulate%20a%20m
>>> atrix> >
>>> Date: Mon, 25 Jun 2007 12:39:46 -0400
>>>
>>> Is this what you want?
>>>
>>>> x
>>>
>>> [,1] [,2] [,3] [,4] [,5]
>>>
>>> [1,] 1 6 11 16 21
>>> [2,] 2 7 12 17 22
>>> [3,] 3 8 13 18 23
>>> [4,] 4 9 14 19 24
>>> [5,] 5 10 15 20 25
>>>
>>>
>>>
>>> cbind(row=as.vector(row(x)), col=as.vector(col(x)),
>> value=as.vector(x))
>>>
>>>
>>> row col value
>>> [1,] 1 1 1
>>> [2,] 2 1 2
>>> [3,] 3 1 3
>>> [4,] 4 1 4
>>> [5,] 5 1 5
>>> [6,] 1 2 6
>>> [7,] 2 2 7
>>> [8,] 3 2 8
>>> [9,] 4 2 9
>>> [10,] 5 2 10
>>> [11,] 1 3 11
>>> [12,] 2 3 12
>>> [13,] 3 3 13
>>> [14,] 4 3 14
>>> [15,] 5 3 15
>>> [16,] 1 4 16
>>> [17,] 2 4 17
>>> [18,] 3 4 18
>>> [19,] 4 4 19
>>> [20,] 5 4 20
>>> [21,] 1 5 21
>>> [22,] 2 5 22
>>> [23,] 3 5 23
>>>
>>>
>>> [24,] 4 5 24
>>> [25,] 5 5 25
>>>>
>>>
>>> On 6/25/07, Jon Hak <Jon_Hak_at_natureserve.org> wrote:
>>>>
>>>> I have read everything I can find on how to manipulate a results
>>> matrix in
>>>
>>
> <http://tolstoy.newcastle.edu.au/R/e2/help/07/06/19875.html#19887qlink1>
>>>
>>>> R and I have to admit I'm stumped. I have set up a process to
> extract
>>> a
>>>> dataset from ArcGIS to compute a similarity index (Jaccards) in
>> Vegan.
>>> The
>>>> dataset is fairly simple, but large, and consists of rows = sample
>>> area, and
>>>> columns = elements. I've been able to view the results in R, but I
>>> want to
>>>> get the results out to a database and a matrix that is 6000-rows x
>>>> 6000-columns can be very difficult to manipulate in Windows XP. I
>>> would to
>>>> rotate the matrix so that the output would look like the old
>> condensed
>>>
>>>> format in programs like Conoco. Ideally, I would like format to look
>>>> something like this;
>>>>
>>>>
>>>> Site-row Site-col Jaccard
>>>> 1 1 1
>>>> 1 2 .9
>>>> 1 3 .6
>>>> 2 1 .9
>>>> 2 2 1
>>>> 2 3 .75
>>>>
>>>> Thanks for any help,
>>>>
>>>>
>>>>
>>>>
>>>> ***********************************************************
>>>> John Hak
>>>> Senior GIS Analyst/Sr. Ecologist
>>>> NatureServe
>>>> 4001 Discovery Drive
>>>> Boulder, CO 80303
>>>> (703) 797-4809
>>>>
>>>> There is perhaps no better demonstration of the folly of human
>>> conceits
>>>> than this distant image of our tiny world. To me, it underscores our
>>>> responsibility to deal more kindly with one another, and to preserve
>>> and
>>>> cherish the pale blue dot, the only home we've ever known. --Carl
>>> Sagan
>>>>
>>>>
>>>> ______________________________________________
>>>> R-help_at_stat.math.ethz.ch 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.
>>>>
>>>
>>> --
>>> Jim Holtman
>>> Cincinnati, OH
>>> +1 513 646 9390
>>>
>>> What is the problem you are trying to solve?
>>>
>>> [[alternative HTML version deleted]]
>>>
>>>
>>>
>>>
>>>
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>>
>>
>> --
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>>
>> What is the problem you are trying to solve?
>>
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
More information about the R-help
mailing list