jeudi 22 décembre 2016

Ifelse does not work inside a function

I have come across this issue a couple of times, where placing the ifelse statement inside a function always results in the same result.

Data$QuarterHour <- ifelse( Data$Minute < 15, return( 1 ),
                                  ifelse( Data$Minute < 30 , return( 2 ),
                                          ifelse( DataFrame$Minute < 45, return( 3 ), return( 4 ) ) ) )

This works perfectly fine. However, when I place it into a function. It does not.

calculateHourQuarter <- function( aMinute )
{
ifelse( aMinute < 15, return( 1 ),
        ifelse( aMinute < 30 , return( 2 ),
                ifelse( aMinute < 45, return( 3 ), return( 4 ) ) ) )
}

DataFrame$QuarterHour <- calculateHourQuarter( as.numeric( Data$Minute ) )

This results in the column being filled with 1's. When I call the function interactively in the console, it produces the right result when I just pass it integer's from the console.

Any ideas?

Thanks!

Aucun commentaire:

Enregistrer un commentaire