dimanche 29 août 2021

If statement conditional is getting ignored in R

I have a dataframe(df) with 20,000 rows that looks like this:

     type letter
1     a     a
2     a     k
3     a     j
4     a     c
5     a     p
...  ...   ...
2523  i     v
2524  i     j
2525  i     k
2526  i     b
...  ...   ...
7900  a     p
7901  a     x
7902  a     c
...  ...   ...

I want to create a new column 'match' based on two conditions: (1) MATCH if type==a and letter==a, b, or c (2) MATCH if type==i and letter==i, j, or k

So I ran if statements:

a.letter=c("a", "b", "c")
i.letter=c("i", "j", "k")

if (df$type=="a") {
  df$match <- ifelse(df$letter %in% a.letter, "MATCH", "NO MATCH")
} else if (df$type=="i") {
   df$match <- ifelse(df$letter %in% i.letter, "MATCH", "NO MATCH")
}

My desired output is this:

     type letter match
1     a     a    MATCH
2     a     k    NO MATCH
3     a     j    NO MATCH
4     a     c    MATCH
5     a     p    NO MATCH
...  ...   ...   ...
2523  i     v    NO MATCH
2524  i     j    MATCH
2525  i     k    MATCH
2526  i     b    NO MATCH
...  ...   ...   ...
7900  a     p    NO MATCH
7901  a     x    NO MATCH
7902  a     c    MATCH
...  ...   ...

However, it seems like the second if statement is getting totally ignored. My current output looks like this:

     type letter match
1     a     a    MATCH
2     a     k    NO MATCH
3     a     j    NO MATCH
4     a     c    MATCH
5     a     p    NO MATCH
...  ...   ...   ...
2523  i     v    NO MATCH
2524  i     j    NO MATCH
2525  i     k    NO MATCH
2526  i     b    NO MATCH
...  ...   ...   ...
7900  a     p    NO MATCH
7901  a     x    NO MATCH
7902  a     c    MATCH
...  ...   ...

To troubleshoot, I tried testing with just one if statement and, oddly enough, it would work perfectly fine for the first conditional, but not the second conditional.

This works:

if (df$type=="a") {
 df$match <- 0
}

But this doesn't (no new column created):

if (df$type=="i") {
 df$match <- 0
}

Why would R not recognize my second conditional entirely?

Aucun commentaire:

Enregistrer un commentaire