I'd like some help.
I have a matrix with 21 rows and a previously undefined number of columns (depend of the input). Each cell has a number representing how many times the element of the row appears in a same columns.
structure(c(7L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 29L, 3L, 0L, 0L, 0L, 0L, 0L,
35L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 3L, 0L, 0L, 0L, 0L, 0L, 35L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 3L, 0L, 1L, 0L, 0L, 2L, 0L, 0L,
0L, 0L, 28L, 4L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 2L,
0L, 0L, 0L, 0L, 0L, 37L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 3L, 0L, 0L, 0L, 0L,
1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 29L, 0L, 3L, 1L, 0L, 1L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 7L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 28L,
0L, 0L, 1L, 0L, 0L, 0L, 34L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,
1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 6L, 2L, 22L, 1L, 3L, 2L, 1L, 0L, 0L, 0L, 0L,
2L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 4L, 0L,
28L, 0L, 0L, 3L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 37L, 0L,
0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,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 0L, 0L, 0L, 0L, 39L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 33L, 0L, 2L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L), .Dim = c(22L,
36L), .Dimnames = list(c("-", "A", "C", "D", "E", "F", "G", "H",
"I", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X",
"Y"), NULL))
This is a MultipleAlignment
object from Biostring
package, but I don't care about the type of object, I may convert it any time.
I'd like to extract a matrix from this one where I have only columns
where a same element (a row) represent x% of the column.
Something like value on cell / sum values in a column
.
for example, on column 6 the W
appear 29 times where the sum of values in this column is 39, it means more than 74% or 0,74 (frequency),so if my cutoff is cutoff >= 0.7
it is included on final matrix.
There are a easy way to do this.
I prefer BioConductor
packages (as Biostring
), tidyverse
or even R base
.
Thanks in advance.
Here is one option, where mat
is the input you provided: a 22x36 matrix
thres <- 0.7
out <- mat[, colSums((mat / colSums(mat)) >= thres) > 0]
You'd drop three columns
dim(out)
# [1] 22 33
The idea is to divide the matrix by the column sums and check whether the result is greater then or equal to 0.7. This gives a logical matrix. We can then calculate column sums another time and keep only those columns for which we have at least one TRUE
.
Another idea using apply
as provided by @d.b. in the comments
mat[, apply(mat, 2, function(x) any((x/sum(x)) > thres))]