0
votes

I have a symmetric matrix that looks like this (this is a relationship matrix between 6 animals):

     AN1   AN2   AN3   AN4   AN5   AN6
AN1   1    0.5    0   0.25    0    0.5
AN2  0.5    1     0    0.5    0     0
AN3   0     0     1   0.25    0     0
AN4  0.25  0.5  0.25    1    0.5    0
AN5   0     0     0    0.5    1     0
AN6  0.5    0     0     0     0     1

I would like to subset this matrix and select only some animals stored in a 1 dimension dataframe. Here is the matrix that I would like:

     AN1   AN3   AN6
AN1   1     0    0.5
AN3   0     1     0
AN6  0.5    1     1

Here is what I tried:

list_individuals <- as.list(df['ID'])  # create list from the dataframe (the list contains the animals that I want to extract from the matrix: AN1, AN3 and AN6)
matrix2 <- subset(original_maxtrix, rownames(original_maxtrix) %in% list_individuals[["ID"]])
matrix3 <- subset(matrix2, colnames(matrix2) %in% list_individuals[["ID"]])

But I have an issue when selecting the columns (last code line): Error in x[subset & !is.na(subset), vars, drop = drop] : (subscript) logical subscript too long

What am I missing here?

1
df is a data frame with one column containing the ID of the animals that I want to extract from the matrixFalco

1 Answers

1
votes

Try to filter rows and columns in this way:

matrix[rownames(matrix)%in%list_individuals,colnames(matrix)%in%list_individuals]

Only rows and columns contained in list_individuals will be mantained in the output.