determine if a number is greater than elements in a row matrix in R
1
0
Entering edit mode
10.5 years ago
cara78 ▴ 10

Hello,

I have a matrix for example

B <- as.matrix(read.table(text="
77 15 70 44
90 61 36 63
51 10 90 99"))

I have an object called M, which contain 57, 60, 75

I want to compare 57 to each number in the first row, to see if greater equal to or less then them.

Then 60 to each number in the second row , to see if greater equal to or less then them. And keep going like that.

If greater output 1 and less then output 0.

I have tried but everything is way off, like cal <- lapply(B, 1, function(x) x<- M <= B)

Could anyone direct me to information on this or offer suggestion on how might be done.

Thanks

bioconductor R compare-numbers Matrix • 18k views
ADD COMMENT
3
Entering edit mode
10.5 years ago

Without context, this is just a simple programming question. Given the tags you used, I'm guessing that you want to do this in the context of filtering expression or count data (or something along those lines).

B <- as.matrix(read.table(text="
77 15 70 44
90 61 36 63
51 10 90 99"))

A <- c(57,60,75)

cal <- t(apply(cbind(A,B), 1, function(x) {
    A=x[1]
    x=x[-1]
    ifelse(x>A, 1, ifelse(x<A, 0, NA))
}))

Edit: I should note that this will yield NA whenever the compared values are equal, since you didn't specify how to handle that. You could also use mapply() after split()ing B, but this is likely simpler.

ADD COMMENT
0
Entering edit mode

Thanks very much. Just Quick question, what does

 A=x[1]
 x=x[-1]

Mean /do ?

I can follow the rest.

ADD REPLY
2
Entering edit mode

It's first good to note what cbind(A,B) is doing. B is a matrix and the cbind() command is just making a new matrix of A and B with A as the first column. Why might someone want to do this? Because then you can use apply() and you automatically have all the information available in the function being applied. Were one not to do this, then either you'd have to use mapply() and split things yourself or completely restructure things such that you apply over a vector of row indices and then subset A and B inside the applied function (odds are good that this is slower). I should note that all of this is only needed because R has TERRIBLE loop performance. In any other language you'd just for(i=0; i<nrow(B);i++) {...}, which is probably more intuitive if you know any other languages.

So then what is the x that function(x) is receiving? For the first iteration, it'll be c(57,77,15,70,44), then c(60,90,61,36,63) and finally c(75,51,10,90,99). In other words, the threshold value that you want to compare things against is x[1]. So A=x[1] just saves that value to a variable and x=x[-1] then removes it from x, since we don't want to compare the number to itself. I should note that this isn't absolutely required, but either you do it in the function or afterwards.

The t() function transposes a matrix. If you don't know what that means, then just run that step with/without the t() function and compare the results (it'll be immediately obvious to you what transposing a matrix means). This is only needed because of how the apply function works. Since function(x) always returns a vector of the same length (n), the output of apply() is a matrix with n rows and columns equal to the number of rows in the input (i.e., it's transposed). This kind of output can sometimes be very useful, but probably not in your case, so I transposed the output.

ADD REPLY

Login before adding your answer.

Traffic: 2537 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6