2

我正在使用具有以下结构的数据集...

grades <- c("7A", "8B", "6C", "6B+")

...但是,我的数据集中没有一些当前未观察到的级别。但我不希望自动定义因子(因此在读取我的数据时使用 read.csv(..., stringsAsFactors=FALSE) )。我想明确定义级别及其标签,并将导入的字符串转换为有序因子,以便如果没有观察到,所有等级都用相关的零计数表示。

real.grades  <- ordered(x = character(), 
                        levels = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17),                       
                        labels = c("6A", "6A+", "6B", "6B+", "6C", "6C+", "7A", "7A+", "7B", "7B+", "7C", "7C+", "8A", "8A+", "8B", "8B+", "8C"))

...但我正在为如何做到这一点而苦苦挣扎?

收到建议和指点,先谢谢了。

4

1 回答 1

2

我想这就是你所追求的:

grades <- c("7A", "8B", "6C", "6B+")

real.grades  <- factor(grades, levels = c("6A", "6A+", "6B", "6B+", "6C", 
    "6C+", "7A", "7A+", "7B", "7B+", "7C", "7C+", "8A", "8A+", "8B", 
    "8B+", "8C"))   

产量:

> real.grades 
[1] 7A  8B  6C  6B+
Levels: 6A 6A+ 6B 6B+ 6C 6C+ 7A 7A+ 7B 7B+ 7C 7C+ 8A 8A+ 8B 8B+ 8C

对于数字表示,请使用:

as.numeric(real.grades)
于 2012-10-11T15:21:31.300 回答