[R] stack or dissolve matrix without double entries
jim holtman
jholtman at gmail.com
Thu Oct 9 19:40:33 CEST 2008
Is this what you want:
> x <- matrix(1:16,4)
> rownames(x) <- colnames(x) <- LETTERS[1:4]
> x
A B C D
A 1 5 9 13
B 2 6 10 14
C 3 7 11 15
D 4 8 12 16
> require(reshape)
> # create single list
> z <- melt(x)
> z
X1 X2 value
1 A A 1
2 B A 2
3 C A 3
4 D A 4
5 A B 5
6 B B 6
7 C B 7
8 D B 8
9 A C 9
10 B C 10
11 C C 11
12 D C 12
13 A D 13
14 B D 14
15 C D 15
16 D D 16
> # now extract just the lower matrix
> z[as.vector(lower.tri(x)),]
X1 X2 value
2 B A 2
3 C A 3
4 D A 4
7 C B 7
8 D B 8
12 D C 12
On Thu, Oct 9, 2008 at 11:31 AM, Marten Winter <marten.winter at ufz.de> wrote:
> Heja,
>
> I've some bigger distance-matrices like this:
>
> num [1:3231, 1:3231] 0.000 0.176 0.176 0.176 0.176 ...
> - attr(*, "dimnames")=List of 2
> ..$ : chr [1:3231] "A" "B" "C" "D" ...
> ..$ : chr [1:3231] A" "B" "C" "D" .
>
> I actually want to convert them into a 2 column dataframe like this:
>
>
> data.frame': 10439361 obs. of 2 variables:
> $ dist: num 0.000 0.176 0.176 0.176 0.176 ...
> $ spec: Factor w/ 219680 levels "A B",..: 1508 26 27 60 61 62 63 340
> 413 414 ...
>
> preferred appearance:
>
> values species
> 1 0.0000000 A A
> 2 0.1761717 A B
> 3 0.1761717 A C
> 4 0.1761717 A D
> 5 0.1761717 A E
>
>
> BUT, I don't need the entries where I have the same distance: "A B" vs.
> "B A", cause the inflate heavily my workspace.
> I know "stack", but I don't know, why but it seems that stack has
> limitations in size (?), it doesn't work.
> I never really understood "reshape".
>
> May someone has a nice way to reshape ;O) a matrix.
>
> THANKS,
> cheers from autumnlike Halle,
> Marten
>
> --
>
> *Marten Winter*
>
> Helmholtz Centre for Environmental Research -- UFZ
> Department of Community Ecology
> Helmholtz-Zentrum für Umweltforschung GmbH - UFZ
> Sektion Biozoenoseforschung
> Theodor-Lieser-Str. 4
> D-06120 Halle (Saale)
> Germany
>
>
> phone: ++49 (0) 345 558-5316 / fax:++49 (0) 345 558-5329
> E-mail: Marten.Winter at ufz.de
> http://www.ufz.de/index.php?en=7081
> DAISIE - Delivering Alien Invasive Species Inventories for Europe
> http://www.europe-aliens.org
>
>
>
> [[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 that you are trying to solve?
More information about the R-help
mailing list