在R

时间:2018-01-09 19:19:13

标签: r discretization

我在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

我想将此向量离散化为名为A,B,C,D,E的范围类,并将NAs值插入另一个名为“NA values”的类中。然后我想把它转换成一个因素。我遵循的流程是下一个:

# 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)

这会产生以下因素:

[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

我怎样才能得到以下结果(使用NAVal级别和NAVal值)?

[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

0 个答案:

没有答案