zoukankan      html  css  js  c++  java
  • R语言中的因子

                        col1

                            [1] Green Green Red Yellow Green Yellow Yellow Red Yellow
                             Levels: Green Red Yellow


               col2 <- factor(colour, levels = c('G', 'R', 'Y'), labels = c('1', '2', '3'))
               col_vec <- as.vector(col2) #转换成字符向量
                "1" "1" "2" "3" "1" "3" "3" "2" "3"
               col_num <- as.numeric(col2) #转换成数字向量
                 1 1 2 3 1 3 3 2 3
               col3 <- factor(colour, levels = c('G', 'R'))

                 [1] G G R <NA> G <NA> <NA> R <NA>

                 Levels: G R


    2、创建一个有序因子
        例1:score <- c('A', 'B', 'A', 'C', 'B')
                score1 <- ordered(score, levels = c('C', 'B', 'A'));
               score1

                 [1] A B A C B
                 Levels: C < B < A



    3、用cut()函数将一般的数据转换成因子或有序因子。
        例1:exam <- c(98, 97, 52, 88, 85, 75, 97, 92, 77, 74, 70, 63, 97, 71, 98,
                       65, 79, 74, 58, 59, 60, 63, 87, 82, 95, 75, 79, 96, 50, 88)
                exam1 <- cut(exam, breaks = 3) #切分成3组
                exam2 <- cut(exam, breaks = c(0, 59, 69, 79, 89, 100)) #切分成自己设置的组
                attr(exam1, 'levels'); attr(exam2, 'levels'); attr(exam2, 'class')
                ordered(exam2, labels = c('bad', 'ok', 'average', 'good', 'excellent')) #一个有序因子
  • 相关阅读:
    http
    node 学习 http
    socket.io
    每日日报
    每日日报
    每日日报
    06人件读书笔记之一
    每日日报
    每日日报
    05程序员修炼之道:从小工到专家阅读笔记之三
  • 原文地址:https://www.cnblogs.com/gary-bao/p/4533269.html
Copyright © 2011-2022 走看看