rmatrix

How to remove specific rows in a matrix dependent on row conditions


I have a function (from the package asnipe) that has taken my raw data and put it into four massive matrices. Right now I'm only focused on reducing the redundant data in the first one. And here is the function from the package that has given me the matrices I'm working with now:

matrix <- get_associations_points_tw(data.D.all.6, time_window = 300, which_days = 162, 
                           which_locations = NULL)

The matrix shows interactions of individual ID tags within a designated window across time (so each row effectively has its own timestamp, though it isn't shown in the first matrix but rather the second) and right now I am getting a matrix that shows interactions between and individual and themself. The rows that have the true interactions between two individuals output looks something like this:

           01103FE244 01103FE63F 01103FE094 01103FD86E 01103FD80D
01103FD86E          0          0          0          1          1
01103FD80D          0          0          0          1          1

The day 162 was how I was minimizing the matrix so I could actually see what the true interaction rows looked like and how I got the example rows above, just by manually reading the data, finding an interaction, and then changing my script to focus in on that specific timeframe so I could see these rows. I can't maintain this method for the mass amounts of (horribly formatted) data that I do have.

I either need a way to modify the already output matrix to not show the rows with a sum of less than 2, or to make another matrix with the only rows showing the interactions with the only value in the row being the other individual and not both the other individual and the initial individual from that row. (sorry I know that's a lot)

I've tried using an if function to read the rows with a row sum <= 1 and set them to NULL but I'm not the best with R syntax. I also tried to return a matrix with the rows with row sums >= 2 which also didn't work due to my caveman syntax.


Solution

  • From what I understand I see two possibilities: I will demonstrate using a dummy matrix m

    m <- matrix(c(
      1,0,0,0,0,
      0,1,0,0,0,
      0,0,1,0,0,
      0,0,0,1,1,
      0,0,0,1,1
    ), 5, 5)
    
    1. Use rowSums to filter the desired rows

    Simply use rowSums inside the subsetting:

    m[rowSums(m) >= 2,]
    
         [,1] [,2] [,3] [,4] [,5]
    [1,]    0    0    0    1    1
    [2,]    0    0    0    1    1
    

    This returns only the rows that you need.

    2. Use which to find interactions.

    You could use which with the argument arr.ind = TRUE to find interactions:

    subset(as.data.frame(which(m == 1, arr.ind = TRUE)), row != col)
    
      row col
    5   5   4
    6   4   5