0

I have a data frame put together from FRED data, I'm trying to find the proportion of times each variable has indicated a recession correctly. I've mutated the data to give me a T/F output if each variable is indicating a recession.

How do I tell R to look at, for example, if the bondyieldTF is indicating a recession(TRUE), then look at RecessionTF variable to see if (say in the next 4 observations) RecessionTF returns TRUE?

I have yet try try anything because I don't know where to begin (what to look up) to solve my problem.

    bondyieldTF RecessionTF
   <lgl>       <lgl>      
 1 TRUE        FALSE      
 2 TRUE        FALSE      
 3 TRUE        FALSE      
 4 TRUE        TRUE       
 5 TRUE        TRUE       
 6 FALSE       TRUE       
 7 FALSE       TRUE       
 8 FALSE       TRUE       
 9 FALSE       TRUE       
10 FALSE       TRUE       
11 FALSE       FALSE   

So what I want is to look at bondyieldTF, if in row 5, it is TRUE then look at RecessionTF Row 5,6,7,8, if any of those are TRUE, bondyieldTF was correct. If there are no true in RecessionTF Row 5,6,7,8, then BondyieldTF was incorrect.

EconFrame %>% 
  if (bondyieldTF == TRUE){
    if (RecessionTF == TRUE){
      
      Indication = Correct
    } else {
      Indication = Incorrect
    }
  }

This is the best I can come up with, and it says

Error in if (.) bondyieldTF == TRUE else { : the condition has length> 1

GnarTank
  • 1
  • 3
  • 2
    It almost always helps if you can include some example data (fine if it's fake) and an example of the sort of output you're expecting. I don't know what you mean by "telling R to view / look at" values. It sounds like you might be using `dplyr` since you mention `mutate`. Are you looking to add another indicator that, for instance, is TRUE if that row's `yield_curve_recession` is TRUE, and if any of the next 4 values of `Recession` are TRUE? – Jon Spring Apr 05 '23 at 17:39
  • @JonSpring. That sounds like what I'm trying to do. Right now I have: mutate(bondyieldTF = BondYield < 0), to output a T/F into bondyieldTF variable. So I want to be able to say, If bondyieldTF is TRUE, look at RecessionTF next 4 observations and if any are true in RecessionTF then bondyieldTF was correct(TRUE), if not then bondyieldTF was incorrect(FALSE). – GnarTank Apr 05 '23 at 17:57
  • 2
    Gnartank, it's generally preferred to put the sample data (e.g., `dput(head(x))`) and code attempted in your question in [code block]s. Could you [edit] your question and add it there? Some discussions about how to fill out the question to be as reproducible as possible: https://stackoverflow.com/q/5963269 , [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Apr 05 '23 at 17:59

0 Answers0