jeudi 30 mars 2017

if statement within double for loop: specific example [duplicate]

This question already has an answer here:

I'm writing a short code which compares two dataframes- list & knownlocation. I want to know for each item in list, whether it falls within a knownlocation.

colnames(list) <- c("gene_symbol", "chromo", "start", "end")
colnames(knownlocation) <- c("snp", "chr", "s", "e")

To find this I wrote the code to make a new column in "list" saying TRUE or FALSE whether it's in any of knownlocation:

for (i in 1:nrow(list)) {
for (j in 1:nrow(knownlocation)) {
if ( (list[i, 2] == knownlocation[j, 2]) && (list[i, 3] >= knownlocation[j, 3]) && (list[i, 4] <= knownlocation[j, 4]) ) {
list[i, 5] = "TRUE" }
else { list[i, 5] = "FALSE"}
}}

This code looks fine to me and it runs with no errors. Problem is the entire list shows FALSE, even if it does fall within a location in knownlocation. Can anyone find something obviously wrong that I'm missing?

Aucun commentaire:

Enregistrer un commentaire