1

I'm discretizing the following vector in R:

# Generated vector
my_vector <- round(c(seq(from=0, to=50, by=0.83)), 2)

# Generate 9 random indexes
random_indexes <- sample(0:50, 9)

# Assign NA to these indexes
my_vector[random_indexes] <- NA

I want to discretize this vector into range classes with names A,B,C,D,E and insert the NAs values into another class named "NA values". Then I want to convert it in a factor. The process I followed is the next:

# Brakpoints definition
breakpoints <- c(5, 10, 20, 30, 40, 50)

# Labels definition
classes <- c("A", "B", "C", "D", "E")

# Generate the factor with the cut function
cut(my_vector, breaks=breakpoints, labels=classes)

This generates the following factor:

[1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> A    A    <NA> A    <NA> A    <NA> B    B    B   
[18] <NA> <NA> B    B    B    B    B    B    C    C    C    C    C    C    C    C    C   
[35] C    C    C    D    D    D    D    D    <NA> <NA> <NA> D    D    D    D    E    E   
[52] E    E    E    E    E    E    E    E    E    E   
Levels: A B C D E

How could I get this the following (with NAVal level and NAVal values)?

[1] NAVal NAVal NAVal NAVal NAVal NAVal NAVal A    A    NAVal A    NAVal A    NAVal B    B    B   
[18] NAVal NAVal B    B    B    B    B    B    C    C    C    C    C    C    C    C    C   
[35] C    C    C    D    D    D    D    D    NAVal NAVal NAVal D    D    D    D    E    E   
[52] E    E    E    E    E    E    E    E    E    E   
Levels: A B C D E NAVal
Marco
  • 670
  • 5
  • 16

0 Answers0