%in%, == or something else to compare multiple values

Viewed 2133

I think I'm still a little unclear on how R works on individual elements in vectorized statements.

I have the following code

df1$flag <- ifelse(df1$year < 2013 &
        df1$year == df2$year &
        as.character(df1$code) == as.character(df2$code), 'Y', df1$flag)

And I am operating on this data

year <- c(2011, 2012, 2011, 2013, 2014, 2016, 2016, 2015, 2016, 2010)
flag <- 'N'
code <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
df1 <- data.frame(year, flag, code)

rm(year)
rm(flag)
rm(code)

year <- c(2015, 2013, 2011, 2012, 2016, 2016, 2010)
code <- c(5, 7, 3, 2, 14, 99, 10)
df2 <- data.frame(year, code)

df1$flag <- ifelse(df1$year < 2013 &
                     df1$year == df2$year &
                     as.character(df1$code) == as.character(df2$code), 'Y', df1$flag)

I want this to be the output

> df1
   year flag code
1  2011    1    1
2  2012    Y    2
3  2011    Y    3
4  2013    1    4
5  2014    1    5
6  2016    1    6
7  2016    1    7
8  2015    1    8
9  2016    1    9
10 2010    Y   10

But instead I am getting this

> df1
   year flag code
1  2011    1    1
2  2012    1    2
3  2011    Y    3
4  2013    1    4
5  2014    1    5
6  2016    1    6
7  2016    1    7
8  2015    1    8
9  2016    1    9
10 2010    1   10

I want the ifelse statement to compare each element of df1$year and df1$code to each element of df2$year and df2$code, but it doesn't look like == or %in% will do that.

To put it another way, what I want is to compare the elements like this

for(i in 1:nrow(df1)) {
    for(z in 1:nrow(df2)) {
         if(df1$year[i] < 2013 & df1$year[i] == df2$year[z] & 
                            as.character(df1$code[i]) == as.character(df2$code[z]))
             df1$flag[i] <- 'Y'
    }
}

Obviously using for like this GREATLY slows down execution and cannot be used, but it doesn't seem like ==, %in%, identical() or all.equal() will do what I describe in the for loop either. How can I get the output I've described in R?

1 Answers
Related