I wrote a somewhat grotesque function that should just return a vector with two values.
For example, if you enter 33, you must return c(30, 40). It could not have been much easier.
return_a_range <- function(number){
ans <- ifelse( (30 <= number & number <= 40), c(30, 40),
(ifelse( (40 < number & number <= 50), c(40, 50),
(ifelse( (50 < number & number <= 60), c(50, 60),
(ifelse( (60 < number & number <= 70), c(60, 70),
(ifelse( (70 < number & number <= 80), c(70, 80),
(ifelse( (80 < number & number <= 100), c(80, 100),
ans <- c("NA"))))))))))))
return(ans)}
return_a_range(33)
Why does this only return 30? How can I not return c(30, 40)? Why did R decide only to return the value in the first position of the vector?
EDIT
Although most of the answers relate to (justified!) That slap me to write a lousy expression ifelse, I think the real question was acknowledged and best answered by @MrFick in the comments below.
source
share