mercredi 28 juillet 2021

If statement is always true regardless of condition

I think I have the same issue posted here: R Programming: Condition giving always TRUE but I do not know how to apply the answer to my situation.

The if statement below searches for a match in the major_allele column for REF and vice-versa. The problem is that it always evaluates to true as shown by the rows with astericks. The previous answer at the above link stated that ifelse always produces a vector so I am guessing that it is only looking at the first observation, evaluates to true and then moves to doing the same across all rows. However, I would like it to go row-by-row. Below new_gwas_a1 is created but from the REF column for all rows but it should be false for the rows with asterisk.

for (i in files){
rsid.tmp  <- read.table(i, header = TRUE, sep=" ", stringsAsFactors = 
FALSE, fill = TRUE)


rsid.tmp$new_gwas_a1 <-if((sapply(lapply(rsid.tmp$REF, grepl, 
x=rsid.tmp$major_allele),any))
                || (sapply(lapply(rsid.tmp$major_allele, grepl, 
x=rsid.tmp$REF),any)))
        {rsid.tmp$REF}

new_file <- sub("(\\.txt)$", "_updated\\1", i)
write.table(rsid.tmp, new_file, quote = FALSE, row.names = FALSE)


REF ALT minor_allele_frequency  minor_allele    major_allele     
new_gwas_a1
A   G   0.000219914 G   A   A
C   T   0.0144844   T   C   C
C   T   0.0445486   T   C   C
C   T   0.00647968  T   C   C
C   T   0.222656    T   C   C
**A G,T 0.12189     A   G   A
**A G,T 0.305252    A   G   A
C   T   0.00210762  T   C   C
C   A   0.00139373  A   C   C

I was also receiving this warning previously which hasn't shown again since but I wonder if the issue is related. Warning message: In if (sapply(lapply(rsid.tmp$REF, grepl, x = rsid.tmp$minor_allele), : the condition has length > 1 and only the first element will be used

I tried this same code directly in R and it seems to work. See below please

> a <- c("TGTGTGT")
> b <- c("AC")
> c <- c("A")
> d <- c("AGTGTG,ATGTGT")
> e <- c("TG")

> if((sapply(lapply(a, grepl, x=c),any)) || (sapply(lapply(c, grepl, 
x=a),any))) {print(Yes)}

> if((sapply(lapply(b, grepl, x=c),any)) || (sapply(lapply(c, grepl, 
x=b),any))) {print("Yes")}
[1] "Yes"

Aucun commentaire:

Enregistrer un commentaire