I am writing an R-script to check for a particular expression value in a narrow range of values. For example, expression count of Gene A = 0.8. I want the flexibility to be 0.2, therefore if Gene X (the gene under test) = 0.5 to 1.1, I'll consider the expressions to be equal. The R-script I have written has an if-statement nested within a for-loop, and the loop is functioning at times, and sometimes it isn't.
I am pasting the script below to avoid any confusion -
a = 0.8
b= 0.68
a_vec = seq(a-0.3,a+0.3, by=0.001)
for(i in a_vec) {
if(i == b) {
print(b)
}
}
When b = 0.68, 0.3, 0.35 there is no output, but when b = 0.8, 1, 1.1, 1.03 it prints 0.8, 1, 1.1, 1.03
Hi, one tip. If you want people to read your code, please make your code human readable. We are not machines.
I'm sorry, but what is the problem?
Nothing any more, I modified your post :)
Thanks for the modification, Devon :)
This looks more legible!
I am not sure I understand what you want to do. Is it "if your value(gene X) is comprised between value(gene A) +/- 0.3 you print something like 'TRUE' if not 'FALSE' " ?
If so it would be either to go for a statement like the following one: if(b < a+0.3 | b > a-0.3) print(TRUE) else { print(FALSE)}
I want the value printed, not TRUE or FALSE. I need to use these values further. I need the values printed mainly because I will have a huge collection, and need to know which are the ones present.
You'd be better using Filter() than trying to use print() - especially if you have a huge collection of things to check
If you're using for-loops in R, you're doing it wrong!
I disagree. I know that R fanatics want you to use the apply family of functions, but if R is not the only language you use, the for loop is the most important loop in bioinformatics.