I have a vector of size 217 called "A". the values of A are not sorted and range from 0 to 1 (normalized) I am having difficulty writing a program to create a new vector "B" where if A's value is 0< A <=0.333 then B is 0 if A's value is 0.333< A <=0.666 then B is 1 if A's value is 0.666< A <=1 then B is 2 so if A is 0.22 0.999 0.444 0 B would be 0 2 1 0 thank you,
Classifying values in vector
3 messages · mark salsburg, Sundar Dorai-Raj
mark salsburg wrote:
I have a vector of size 217 called "A". the values of A are not sorted and range from 0 to 1 (normalized) I am having difficulty writing a program to create a new vector "B" where if A's value is 0< A <=0.333 then B is 0 if A's value is 0.333< A <=0.666 then B is 1 if A's value is 0.666< A <=1 then B is 2 so if A is 0.22 0.999 0.444 0 B would be 0 2 1 0 thank you,
Sounds like you are looking for ?cut: > A <- runif(217) > B <- cut(A, c(0, 1/3, 2/3, 1), labels = c(0, 1, 2)) > # convert factor to numeric > as.numeric(levels(B)[B]) HTH, --sundar
mark salsburg wrote:
I have a vector of size 217 called "A". the values of A are not sorted and range from 0 to 1 (normalized) I am having difficulty writing a program to create a new vector "B" where if A's value is 0< A <=0.333 then B is 0 if A's value is 0.333< A <=0.666 then B is 1 if A's value is 0.666< A <=1 then B is 2 so if A is 0.22 0.999 0.444 0 B would be 0 2 1 0 thank you,
Didn't look closely at your example. If "A" can == zero then you need to include "include.lowest = TRUE" in your call to cut. This would mean your breaks are: if A's value is 0 <= A <=0.333 then B is 0 which is different from what you have written above. --sundar