Search code examples
rmatrixsubsampling

Subsample a matrix by selection locations with specific values within a matrix in R


I'm have to use R instead of Matlab and I'm new to it.

I have a large array of data repeating like 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10...

I need to find the locations where values equal to 1, 4, 7, 10 are found to create a sample using those locations.

In this case it will be position(=corresponding value) 1(=1) 4(=4) 7(=7) 10(=10) 11(=1) 14(=4) 17(=7) 20(=10) and so on.

in MatLab it would be y=find(ismember(x,[1, 4, 7, 10 ])), Please, help! Thanks, Pavel


Solution

  • something like this?

    foo <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    bar <- c(1, 4, 7, 10)
    which(foo %in% bar)
    #> [1]  1  4  7 10 11 14 17 20
    

    @nicola, feel free to copy my answer and get the recognition for your answer, simply trying to close answered questions.