I have the following vector:
set.seed(1)
y<-sample(1:50, 20, T)
And I want to categorize them and create a new vector. That is, if the value of y
is between 1 and 15, assign the value of 1 to the vector x
. Likewise, if it is between 16 and 35, assign x
the value of 2 to the vector and finally, if it is in the range of 36 to 50, assign the value of 3 to the vector x
.
All using the if else conditional.
if("Condición 1") {
"Función a ejecutar si Condición 1 es verdadero."
} else if("Condición 2") {
"Función a ejecutar si Condición 2 es verdadero."
} else {
"Función a ejecutar si Condición 1 y Condición 2 son falsas."
}
I do not know how to do it. If I know how to do it if I apply the function ifelse()
or with case_when()
. The doubt arises as to how to do it with that syntax, since in essence what the ifelse()
and the case_when()
do is the same.
Thank you very much in advance
First of all, the
if
common one is not the most suitable for this, since it really only evaluates a single condition, to use it you would need to implement a loop for each value of the vector and check the condition in each one.It is preferable to use, as you say, the
ifelse()
one that is vectorized, but since you have three conditions, you need to "nest" theifelse()
, for example:We validate the first range, if it is not met, we validate the second and so on, if we had more. In any case, even simpler, is to use
findInterval()
where we simply have to define the initial value of each range and the function simply does not return the number of these:This is an option forcing the iteration over the vector
y
withsapply()
and an anonymous function.Although it could be expected that since the input is a vector, the function would operate in a vectorized way, in this case it is necessary to explicitly iterate with
sapply()
. A wouldfor
also work. The risk of usingsapply()
it in scripts whose execution you are not going to be supervising is that the output changes structure to accommodate the data. In the case of the example, it returns a vector of the classcharacter
, but if any value ofy
does not meet any of the conditions (in this case it hardly occurs because of theelse
) then it will silently return a list to be able to accommodate theNULL
. So use carefully!If you go this route you could use
vapply()
instead ofsapply()
, so that it does an output type check and, in case the output is not what you expected, it returns an error instead of a list/array or whateversapply()
you fancy come back to usThe argument
FUN.VALUE =
specifies the type of output. If it is not fulfilled, it gives an error. Let's compare the following cases, where I remove the trailingelse
, some values ofy
are evaluated asNULL
andsapply()
produces a list:It returns an error.
It returns a list.