2

I have a very strange thing happening in R. This code gives the following error, "Error in 1 && CurrentData$phase[i] > -0.5 || PriorData$phase[MatchTimeFrames(i, : target of assignment expands to non-language object"

  return (CurrentData$phase[i-1]<-1 && CurrentData$phase[i]>-0.5 || 
         PriorData$phase[MatchTimeFrames(i, CurrentData, PriorData)-1]<-1 &&
         PriorData$phase[MatchTimeFrames(i, CurrentData, PriorData)] > -0.5 ||  
         CurrentData$phase[i]>0 ||
         PriorData$phase[MatchTimeFrames(i, CurrentData, PriorData)] >0
       )

If I comment out the first condition, the code runs.

  return (#CurrentData$phase[i-1]<-1 && CurrentData$phase[i]>-0.5 || 
         PriorData$phase[MatchTimeFrames(i, CurrentData, PriorData)-1]<-1 &&
         PriorData$phase[MatchTimeFrames(i, CurrentData, PriorData)] > -0.5 ||  
         CurrentData$phase[i]>0 ||
         PriorData$phase[MatchTimeFrames(i, CurrentData, PriorData)] >0
       )

If I comment out the second condition, the code runs.

  return (CurrentData$phase[i-1]<-1 && CurrentData$phase[i]>-0.5 || 
         #PriorData$phase[MatchTimeFrames(i, CurrentData, PriorData)-1]<-1 &&
         PriorData$phase[MatchTimeFrames(i, CurrentData, PriorData)] > -0.5 ||  
         CurrentData$phase[i]>0 ||
         PriorData$phase[MatchTimeFrames(i, CurrentData, PriorData)] >0
       )

Obviously, I want both conditions to run and what made it work was putting a space between the less than sign and the -1 on the second condition.

 return (CurrentData$phase[i-1]<-1 && CurrentData$phase[i]>-0.5 || 
         PriorData$phase[MatchTimeFrames(i, CurrentData, PriorData)-1]< -1 && PriorData$phase[MatchTimeFrames(i, CurrentData, PriorData)] > -0.5 ||  
         CurrentData$phase[i]>0 ||
         PriorData$phase[MatchTimeFrames(i, CurrentData, PriorData)] >0
       )

Any idea why that is happening? It seems very strange that in this particular instance R is requiring a space between the less than sign and -1. Notice that the first term, CurrentData$phase[i-1]<-1, has no space.

Thank you.

Ricardo Oliveros-Ramos
  • 4,322
  • 2
  • 25
  • 42
mks212
  • 901
  • 1
  • 18
  • 40

1 Answers1

4

It's because <- is assignment. Here is a shorter example, which makes it more clear what is happening:

> x <- 1 && y <- 2
Error in 1 && y <- 2 : 
  target of assignment expands to non-language object
Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112