Replacement values of a dist object with either ij or position indexing

dist_replace(object, i, j, value, k)

Arguments

object

dist object

i

(integer vector) row positions

j

(integer vector) column positions

value

(integer/numeric vector) Values to replace

k

(integer vector) positions

Value

dist object

Details

There are two modes to specify the positions:

  • ij-mode where i and j are specified and k is missing. If i or j are missing, they are interpreted as all values of i or j (similar to matrix or dataframe subsetting). Lengths of i, j are required to be same. If 'value' is singleton, then it is extended to the length of i or j. Else, 'value' should have same length as i or j.

  • k-mode where k is present and both i and k are missing. k is the positions in the dist object. If 'value' is singleton, then it is extended to the length of k. Else, 'value' should have same length as k.

Examples

# create a dist object d <- dist(iris[,1:4]) attr(d, "Labels") <- outer(letters, letters, paste0)[1:150] head(d)
#> [1] 0.5385165 0.5099020 0.6480741 0.1414214 0.6164414 0.5196152
max(d)
#> [1] 7.085196
as.matrix(d)[1:5, 1:5]
#> aa ba ca da ea #> aa 0.0000000 0.5385165 0.509902 0.6480741 0.1414214 #> ba 0.5385165 0.0000000 0.300000 0.3316625 0.6082763 #> ca 0.5099020 0.3000000 0.000000 0.2449490 0.5099020 #> da 0.6480741 0.3316625 0.244949 0.0000000 0.6480741 #> ea 0.1414214 0.6082763 0.509902 0.6480741 0.0000000
# replacement in ij-mode d <- dist_replace(d, 1, 2, 100) dist_extract(d, 1, 2, product = "inner")
#> [1] 100
d <- dist_replace(d, "ca", "ba", 102) dist_extract(d, "ca", "ba", product = "inner")
#> [1] 102
d <- dist_replace(d, 1:5, 6:10, 11:15) dist_extract(d, 1:5, 6:10, product = "inner")
#> [1] 11 12 13 14 15
d <- dist_replace(d, c("ca", "da"), c("aa", "ba"), 102) dist_extract(d, c("ca", "da"), c("aa", "ba"), product = "inner")
#> [1] 102 102
# replacement in k-mode d <- dist_replace(d, k = 2, value = 101) dist_extract(d, k = 2)
#> [1] 101
dist_extract(d, 3, 1, product = "inner") # extracting k=2 in ij-mode
#> [1] 101