Skip to contents

Operators acting on objects to extract or replace parts.

Usage

# S4 method for CompositionMatrix
[(x, i, j, ..., drop = TRUE)

# S4 method for CompositionMatrix
[(x, i, j, ...) <- value

# S4 method for CompositionMatrix
[[(x, i, j, ...) <- value

Arguments

x

An object from which to extract element(s) or in which to replace element(s).

i, j

Indices specifying elements to extract or replace. Indices are numeric, integer or character vectors or empty (missing) or NULL. Numeric values are coerced to integer as by as.integer() (and hence truncated towards zero). Character vectors will be matched to the name of the elements. An empty index (a comma separated blank) indicates that all entries in that dimension are selected.

...

Currently not used.

drop

A logical scalar: should the result be coerced to the lowest possible dimension? This only works for extracting elements, not for the replacement.

value

A possible value for the element(s) of x.

Value

A subsetted object of the same sort as x.

See also

Other mutators: groups, samples, totals

Author

N. Frerebeau

Examples

## Coerce to compositional data
data("hongite")
coda <- as_composition(hongite)

## codaccess
dim(coda) # Get the matrix dimensions
#> [1] 25  5
row(coda) # Get the row indexes
#>       [,1] [,2] [,3] [,4] [,5]
#>  [1,]    1    1    1    1    1
#>  [2,]    2    2    2    2    2
#>  [3,]    3    3    3    3    3
#>  [4,]    4    4    4    4    4
#>  [5,]    5    5    5    5    5
#>  [6,]    6    6    6    6    6
#>  [7,]    7    7    7    7    7
#>  [8,]    8    8    8    8    8
#>  [9,]    9    9    9    9    9
#> [10,]   10   10   10   10   10
#> [11,]   11   11   11   11   11
#> [12,]   12   12   12   12   12
#> [13,]   13   13   13   13   13
#> [14,]   14   14   14   14   14
#> [15,]   15   15   15   15   15
#> [16,]   16   16   16   16   16
#> [17,]   17   17   17   17   17
#> [18,]   18   18   18   18   18
#> [19,]   19   19   19   19   19
#> [20,]   20   20   20   20   20
#> [21,]   21   21   21   21   21
#> [22,]   22   22   22   22   22
#> [23,]   23   23   23   23   23
#> [24,]   24   24   24   24   24
#> [25,]   25   25   25   25   25
col(coda, as.factor = TRUE) # Get the column indexes
#>       [,1] [,2] [,3] [,4] [,5]
#>  [1,] A    B    C    D    E   
#>  [2,] A    B    C    D    E   
#>  [3,] A    B    C    D    E   
#>  [4,] A    B    C    D    E   
#>  [5,] A    B    C    D    E   
#>  [6,] A    B    C    D    E   
#>  [7,] A    B    C    D    E   
#>  [8,] A    B    C    D    E   
#>  [9,] A    B    C    D    E   
#> [10,] A    B    C    D    E   
#> [11,] A    B    C    D    E   
#> [12,] A    B    C    D    E   
#> [13,] A    B    C    D    E   
#> [14,] A    B    C    D    E   
#> [15,] A    B    C    D    E   
#> [16,] A    B    C    D    E   
#> [17,] A    B    C    D    E   
#> [18,] A    B    C    D    E   
#> [19,] A    B    C    D    E   
#> [20,] A    B    C    D    E   
#> [21,] A    B    C    D    E   
#> [22,] A    B    C    D    E   
#> [23,] A    B    C    D    E   
#> [24,] A    B    C    D    E   
#> [25,] A    B    C    D    E   
#> Levels: A B C D E
nrow(coda) # Get the number of rows
#> [1] 25
ncol(coda) # Get the number of columns
#> [1] 5
dimnames(coda) # Get the dimension names
#> [[1]]
#>  [1] "H1"  "H2"  "H3"  "H4"  "H5"  "H6"  "H7"  "H8"  "H9"  "H10" "H11" "H12"
#> [13] "H13" "H14" "H15" "H16" "H17" "H18" "H19" "H20" "H21" "H22" "H23" "H24"
#> [25] "H25"
#> 
#> [[2]]
#> [1] "A" "B" "C" "D" "E"
#> 
rownames(coda) <- LETTERS[1:25] # Set the row names
rownames(coda) # Get the rownames
#>  [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
#> [20] "T" "U" "V" "W" "X" "Y"
colnames(coda) <- letters[21:25] # Set the column names
colnames(coda) # Get the column names
#> [1] "u" "v" "w" "x" "y"

## Subset
coda[[1, 1]] # Get the first value
#> [1] 0.488
coda[1] # Get the first value
#> [1] 0.488
coda[, ] # Get all values
#> <CompositionMatrix: 25 x 5>
#>           u         v          w          x          y
#> A 0.4880000 0.3170000 0.03800000 0.06400000 0.09300000
#> B 0.4820000 0.2380000 0.09000000 0.09200000 0.09800000
#> C 0.3700000 0.0910000 0.34200000 0.09500000 0.10200000
#> D 0.5090000 0.2380000 0.07200000 0.10100000 0.08000000
#> E 0.4420000 0.3830000 0.02900000 0.07700000 0.06900000
#> F 0.5230000 0.2620000 0.04200000 0.12500000 0.04800000
#> G 0.4460000 0.3300000 0.04600000 0.12200000 0.05600000
#> H 0.3460000 0.0520000 0.42900000 0.09600000 0.07700000
#> I 0.4120000 0.1170000 0.26700000 0.09600000 0.10800000
#> J 0.4260000 0.4660000 0.00700000 0.05600000 0.04500000
#> K 0.4990000 0.1950000 0.11400000 0.09500000 0.09700000
#> L 0.4520000 0.3730000 0.02700000 0.05500000 0.09300000
#> M 0.3270000 0.0850000 0.38900000 0.08000000 0.11900000
#> N 0.4140000 0.1290000 0.23400000 0.15800000 0.06500000
#> O 0.4620000 0.1750000 0.15800000 0.08300000 0.12200000
#> P 0.3230000 0.0730000 0.40900000 0.12900000 0.06600000
#> Q 0.4320000 0.4430000 0.01000000 0.07800000 0.03700000
#> R 0.4954955 0.3233233 0.03103103 0.08708709 0.06306306
#> S 0.4230000 0.1580000 0.20400000 0.08300000 0.13200000
#> T 0.4460000 0.1150000 0.23800000 0.11600000 0.08500000
#> U 0.4580000 0.1660000 0.16800000 0.12000000 0.08800000
#> V 0.4990000 0.2500000 0.06800000 0.10900000 0.07400000
#> W 0.4860000 0.3400000 0.02500000 0.09400000 0.05500000
#> X 0.4550000 0.1660000 0.17600000 0.09600000 0.10700000
#> Y 0.4590000 0.2490000 0.09700000 0.09800000 0.09700000
coda[1, , drop = FALSE] # Get the first row
#> <CompositionMatrix: 1 x 5>
#>       u     v     w     x     y
#> A 0.488 0.317 0.038 0.064 0.093
coda[, 1:3] # Get the first three column
#> <CompositionMatrix: 25 x 3>
#>           u         v          w
#> A 0.4880000 0.3170000 0.03800000
#> B 0.4820000 0.2380000 0.09000000
#> C 0.3700000 0.0910000 0.34200000
#> D 0.5090000 0.2380000 0.07200000
#> E 0.4420000 0.3830000 0.02900000
#> F 0.5230000 0.2620000 0.04200000
#> G 0.4460000 0.3300000 0.04600000
#> H 0.3460000 0.0520000 0.42900000
#> I 0.4120000 0.1170000 0.26700000
#> J 0.4260000 0.4660000 0.00700000
#> K 0.4990000 0.1950000 0.11400000
#> L 0.4520000 0.3730000 0.02700000
#> M 0.3270000 0.0850000 0.38900000
#> N 0.4140000 0.1290000 0.23400000
#> O 0.4620000 0.1750000 0.15800000
#> P 0.3230000 0.0730000 0.40900000
#> Q 0.4320000 0.4430000 0.01000000
#> R 0.4954955 0.3233233 0.03103103
#> S 0.4230000 0.1580000 0.20400000
#> T 0.4460000 0.1150000 0.23800000
#> U 0.4580000 0.1660000 0.16800000
#> V 0.4990000 0.2500000 0.06800000
#> W 0.4860000 0.3400000 0.02500000
#> X 0.4550000 0.1660000 0.17600000
#> Y 0.4590000 0.2490000 0.09700000