0

I have a custom function that includes the function IF. When I use that function in a data.table, I get a warning that says "the condition has length > 1 and only the first element will be used".

I think the function may be applied to all rows in the column rather than one row at a time as needed, but I am not certain.

Is anyone aware of why this warning appears?

My function is:

HeatIndex<-function(TempC,RH)
{
TFarheit= TempC * 1.8 + 32
if( TFarheit <80  ) {
   Te=TempC /15
   HI = Te/15
} else {
   TA=TempC /11
   HI = TA/125
}
HI= (HI - 32) / 1.8
return(HI )
}

A sample of the data:

HeatINDEX=data.table(Ave_MeanRH=c(0,100), Ave_MeanT=c(10,20))   #create data.table

And applying the function to the data

HeatINDEX[,HI:=HeatIndex(HeatINDEX$Ave_MeanT, HeatINDEX$Ave_MeanRH)]     
Uwe
  • 41,420
  • 11
  • 90
  • 134
Camilo
  • 201
  • 2
  • 6
  • the 'quick' solution is to specify you want it to apply to each row in `by` : `HeatINDEX[,HI:=HeatIndex(Ave_MeanT, Ave_MeanRH), by = 1:nrow(HeatINDEX)] ` – SymbolixAU Dec 12 '17 at 03:49
  • Usually best to use `ifelse` for these kinds of comparisons, rather than `if`. See https://stackoverflow.com/questions/17252905/else-if-vs-ifelse – Marius Dec 12 '17 at 03:51
  • using the "by" parameter still keeps the warning. and in a larger database it takes a lot of time to run the function to all rows – Camilo Dec 12 '17 at 04:03

1 Answers1

0

As suggested in the comments, you can vectorize your heat index function by using ifelse(). This will certainly be faster than computing row-by-row, another solution suggested in the comments.

# Vectorized version of function:
computeHI = function(T_cel, RH) {
    T_far = T_cel * 1.8 + 32
    HI = ifelse(test=T_far < 80, 
                 yes=(T_cel / 15) / 15, 
                  no=(T_cel / 11) / 125)
    HI = (HI - 32) / 1.8
    return(HI)
}

HeatINDEX[,HI:=HeatIndex(Ave_MeanT, Ave_MeanRH), by=seq(2)]
HeatINDEX[,vectorized_HI:=computeHI(Ave_MeanT, Ave_MeanRH)]

HeatINDEX
#    Ave_MeanRH Ave_MeanT        HI vectorized_HI
# 1:          0        10 -17.75309     -17.75309
# 2:        100        20 -17.72840     -17.72840
bdemarest
  • 14,397
  • 3
  • 53
  • 56
  • I wonder if you can speed this up further by avoiding the C -> F and F -> C conversions? – bdemarest Dec 12 '17 at 04:13
  • Just a heads up, these calculated heat index numbers don't seem to make any sense. You may want to double check the math! But at least you know how to vectorize now. – bdemarest Dec 12 '17 at 04:18
  • The function is just a demo to demonstrate the error. For some reason, the calculation is applied to only the first row, when using ifelse – Camilo Dec 12 '17 at 04:28
  • Hard to say what the issue is without examining the input data.table and columns. It's working as expected with the tiny example in my posted answer. – bdemarest Dec 12 '17 at 04:31
  • yes, it is working well. I was using another conditional with &&, and forgot that ifelse likes just one &, thanks – Camilo Dec 12 '17 at 04:52