I am trying to be more elegant in my coding and leveraging the apply functions. I wanted to stick one in a loop like so
for (ind in (ind2fix ))
{
ADPPK[,ind] = mapply(categorize_cov,value = ADPPK[,ind-4], low = ADPPK[,ind-2],high = ADPPK[,ind-1],doserec = ADPPK$EVID)
}
with the function defined as
categorize_cov <- function(value, low, high, doserec)
{
if (doserec == 0)
{
if (value > high)
return("HIGH")
else if (value < low )
return("LOW")
else
return("NORMAL")
}
else
{
return(NA)
}
}
I get the following error
Error in if (value > high) return("HIGH") else if (value < low) return("LOW") else return("NORMAL") :
the condition has length > 1
what is confusing is if I write it out rather than use indexing to specify the column it works as expected?
explicitly specifying the columns works but I have to do this like 20 times which is annoying
mapply(categorize_cov,value = ADPPK$GLU, low = ADPPK$GLUL,high = ADPPK$GLUH,doserec = ADPPK$EVID)
Is there a way I can loop it ?
5