Search code examples
rmatrixjitter

Chaning matrix into matrix with only unique numbers R


How do i change a matrix into a matrix with only unique numbers

I tried this:

dataset <- as.matrix(iris[,1:4])

head(dataset)
     Sepal.Length Sepal.Width Petal.Length Petal.Width
[1,]          5.1         3.5          1.4         0.2
[2,]          4.9         3.0          1.4         0.2
[3,]          4.7         3.2          1.3         0.2
[4,]          4.6         3.1          1.5         0.2
[5,]          5.0         3.6          1.4         0.2
[6,]          5.4         3.9          1.7         0.4

datasetUnique <- jitter(dataset,factor = 0.001)

head(datasetUnique)
     Sepal.Length Sepal.Width Petal.Length Petal.Width
[1,]     5.100001    3.499998     1.399999   0.2000009
[2,]     4.900000    2.999999     1.399999   0.2000016
[3,]     4.700001    3.199999     1.300000   0.1999983
[4,]     4.600001    3.100002     1.500001   0.1999988
[5,]     4.999999    3.599999     1.399998   0.1999989
[6,]     5.399999    3.900000     1.699999   0.3999981

But this not makes all numbers unique. How do i achieve that?


Solution

  • Add more decimals, then they become unique.

    options(digits=10)
    
    dataset <- as.matrix(iris[,1:4])
    datasetUnique <- jitter(dataset,factor = 0.001)
    
    > head(datasetUnique)
         Sepal.Length Sepal.Width Petal.Length  Petal.Width
    [1,]  5.099982209 3.500005068  1.399988968 0.2000082396
    [2,]  4.900019249 2.999988556  1.399985779 0.2000159874
    [3,]  4.699981033 3.199993951  1.300013794 0.1999958768
    [4,]  4.600000369 3.099985655  1.499996616 0.1999957113
    [5,]  5.000014609 3.599997256  1.400002512 0.1999995772
    [6,]  5.399992065 3.900010935  1.700003428 0.3999821202
    

    A second possibility is using non-random numbers to prevent "bad luck".

    options(digits=10)
    
    dataset          <- as.matrix(iris[,1:4])
    increment_matrix <- matrix(seq( from = 0, to = 0.001, length.out =  ncol(dataset)* nrow(dataset) ) , ncol = ncol(dataset))
    datasetUnique    <- dataset + increment_matrix