upperTriangle: Extract or replace the upper/lower triangular portion of a...

Description Usage Arguments Value Note Author(s) See Also Examples

View source: R/upperTriangle.R

Description

Extract or replace the upper/lower triangular portion of a matrix

Usage

1
2
3
4
upperTriangle(x, diag=FALSE, byrow=FALSE)
upperTriangle(x, diag=FALSE, byrow=FALSE) <- value
lowerTriangle(x, diag=FALSE, byrow=FALSE)
lowerTriangle(x, diag=FALSE, byrow=FALSE) <- value

Arguments

x

Matrix

diag

Logical. If TRUE, include the matrix diagonal.

byrow

Logical. If FALSE, return/replace elements in column-wise order. If TRUE, return/replace elements in row-wise order.

value

Either a single value or a vector of length equal to that of the current upper/lower triangular. Should be of a mode which can be coerced to that of x.

Value

upperTriangle(x) and lowerTriangle(x) return the upper or lower triangle of matrix x, respectively. The assignment forms replace the upper or lower triangular area of the matrix with the provided value(s).

Note

By default, the elements are returned/replaced in R's default column-wise order. Thus

1

will not yield a symmetric matrix. Instead use:

1
  lowerTriangle(x) <- upperTriangle(x, byrow=TRUE)

or equivalently:

1
  lowerTriangle(x, byrow=TRUE) <- upperTriangle(x)

Author(s)

Gregory R. Warnes greg@warnes.net

See Also

diag, lower.tri, upper.tri

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  x <- matrix( 1:25, nrow=5, ncol=5)
  x
  upperTriangle(x)
  upperTriangle(x, diag=TRUE)
  upperTriangle(x, diag=TRUE, byrow=TRUE)


  lowerTriangle(x)
  lowerTriangle(x, diag=TRUE)
  lowerTriangle(x, diag=TRUE, byrow=TRUE)

  upperTriangle(x) <- NA
  x

  upperTriangle(x, diag=TRUE) <- 1:15
  x

  lowerTriangle(x) <- NA
  x

  lowerTriangle(x, diag=TRUE) <- 1:15
  x

  ## Copy lower triangle into upper triangle to make 
  ## the matrix (diagonally) symmetric
  x <- matrix(LETTERS[1:25], nrow=5, ncol=5, byrow=TRUE)
  x
  lowerTriangle(x) = upperTriangle(x, byrow=TRUE)
  x

Example output

sh: 1: cannot create /dev/null: Permission denied
gdata: Unable to locate valid perl interpreter
gdata: 
gdata: read.xls() will be unable to read Excel XLS and XLSX files
gdata: unless the 'perl=' argument is used to specify the location of a
gdata: valid perl intrpreter.
gdata: 
gdata: (To avoid display of this message in the future, please ensure
gdata: perl is installed and available on the executable search path.)
sh: 1: cannot create /dev/null: Permission denied
gdata: Unable to load perl libaries needed by read.xls()
gdata: to support 'XLX' (Excel 97-2004) files.

gdata: Unable to load perl libaries needed by read.xls()
gdata: to support 'XLSX' (Excel 2007+) files.

gdata: Run the function 'installXLSXsupport()'
gdata: to automatically download and install the perl
gdata: libaries needed to support Excel XLS and XLSX formats.

Attaching package: 'gdata'

The following object is masked from 'package:stats':

    nobs

The following object is masked from 'package:utils':

    object.size

The following object is masked from 'package:base':

    startsWith

     [,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
 [1]  6 11 12 16 17 18 21 22 23 24
 [1]  1  6  7 11 12 13 16 17 18 19 21 22 23 24 25
 [1]  1  6 11 16 21  7 12 17 22 13 18 23 19 24 25
 [1]  2  3  4  5  8  9 10 14 15 20
 [1]  1  2  3  4  5  7  8  9 10 13 14 15 19 20 25
 [1]  1  2  7  3  8 13  4  9 14 19  5 10 15 20 25
     [,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
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    4    7   11
[2,]    2    3    5    8   12
[3,]    3    8    6    9   13
[4,]    4    9   14   10   14
[5,]    5   10   15   20   15
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    4    7   11
[2,]   NA    3    5    8   12
[3,]   NA   NA    6    9   13
[4,]   NA   NA   NA   10   14
[5,]   NA   NA   NA   NA   15
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    4    7   11
[2,]    2    6    5    8   12
[3,]    3    7   10    9   13
[4,]    4    8   11   13   14
[5,]    5    9   12   14   15
     [,1] [,2] [,3] [,4] [,5]
[1,] "A"  "B"  "C"  "D"  "E" 
[2,] "F"  "G"  "H"  "I"  "J" 
[3,] "K"  "L"  "M"  "N"  "O" 
[4,] "P"  "Q"  "R"  "S"  "T" 
[5,] "U"  "V"  "W"  "X"  "Y" 
     [,1] [,2] [,3] [,4] [,5]
[1,] "A"  "B"  "C"  "D"  "E" 
[2,] "B"  "G"  "H"  "I"  "J" 
[3,] "C"  "H"  "M"  "N"  "O" 
[4,] "D"  "I"  "N"  "S"  "T" 
[5,] "E"  "J"  "O"  "T"  "Y" 

gdata documentation built on May 2, 2019, 5:49 p.m.