I am unable to do an IF comparison of objects. When I compare them with numeric values the IF works, e.g.: if (c1 < c2){do something} fails if (c1 < 0 ){do something} works.
This is the code I am working with which is showing errors, where HighValues is a list of numbers and LowValue is a list of numbers as well. Is the comparison I am doing wrong?
checkHS<-function(HighValues,LowValues)
{
counter<-1
patternList<-c()
while(counter < length(HighValues))
{
cmpValue1<- 0.15*HighValues[[counter]]
cmpValue2<- HighValues[[counter+1]]
if(cmpValue1<cmpValue2)
patternList<-c(patternList,counter)
counter<-counter+1
}
return (patternList)
}
What am I doing wrong?
Edit: Adding a data set that i am passing to the function for more clarity.
HighValues<-c(1:100)
LowValues<-c(2:101)
When I run the function with the data you’ve supplied, it evaluates to completion without complaint, returning just the values it should:
That said, the following code does the same thing as the function you’ve posted. It takes advantage of R’s ability to ‘vectorize’ calculations, resulting in code that’s more expressive and easier to read.