您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關R語言中Factor類型變量怎么用的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
factor類型的創建
> credit_rating <- c("BB", "AAA", "AA", "CCC", "AA", "AAA", "B", "BB") #生成名為credit_rating的字符向量 > credit_factor <- factor(credit_rating) # step 2.將credit_rating轉化為因子 > credit_factor [1] BB AAA AA CCC AA AAA B BB Levels: AA AAA B BB CCC > str(credit_rating) #調用str()函數,顯示credit_rating結構 chr [1:8] "BB" "AAA" "AA" "CCC" "AA" "AAA" "B" "BB" > str(credit_factor) #調用str()函數,顯示credit_factor結構 Factor w/ 5 levels "AA","AAA","B",..: 4 2 1 5 1 2 3 4
上述代碼中第二個運行后得到了levals,用于顯示不同的因子(不重復),上述代碼運行一二行
>credit_rating <- c("BB", "AAA", "AA", "CCC", "AA", "AAA", "B", "BB") > credit_factor <- factor(credit_rating) # step 2.將credit_rating轉化為因子 > credit_factor [1] BB AAA AA CCC AA AAA B BB Levels: AA AAA B BB CCC > levels(credit_factor) [1] "AA" "AAA" "B" "BB" "CCC" >levels(credit_factor) <-c("2A","3A","1B","2B","3C") > credit_factor [1] 2B 3A 2A 3C 2A 3A 1B 2B Levels: 2A 3A 1B 2B 3C
> summary(credit_rating) Length Class Mode 8 character character > summary(credit_factor) AA AAA B BB CCC 2 2 1 2 1
# 使用plot()將credit_factor可視化 plot(credit_factor) #> summary(credit_factor) # AA AAA B BB CCC # 2 2 1 2 1
1
>AAA_rank <- sample(seq(1:100), 50, replace = T) > AAA_rank [1] 90 28 63 57 96 41 93 70 76 36 26 1 86 43 47 15 23 70 [19] 63 1 79 100 20 59 17 23 84 96 21 33 32 19 52 58 81 37 [37] 22 58 42 75 41 64 15 58 63 2 1 65 54 35 > # step 1:使用cut()函數為AAA_rank創建4個組 > AAA_factor <- cut(x = AAA_rank , breaks =c(0,25,50,75,100) ) > > AAA_factor [1] (75,100] (25,50] (50,75] (50,75] (75,100] (25,50] (75,100] (50,75] [9] (75,100] (25,50] (25,50] (0,25] (75,100] (25,50] (25,50] (0,25] [17] (0,25] (50,75] (50,75] (0,25] (75,100] (75,100] (0,25] (50,75] [25] (0,25] (0,25] (75,100] (75,100] (0,25] (25,50] (25,50] (0,25] [33] (50,75] (50,75] (75,100] (25,50] (0,25] (50,75] (25,50] (50,75] [41] (25,50] (50,75] (0,25] (50,75] (50,75] (0,25] (0,25] (50,75] [49] (50,75] (25,50] Levels: (0,25] (25,50] (50,75] (75,100] > # step 2:使用levels()按順序將級別重命名 > levels(AAA_factor) <- c("low","medium","high","very_high") > > # step 3:輸出AAA_factor > AAA_factor [1] medium medium very_high high very_high high high [8] high medium medium very_high high medium very_high [15] medium low medium low high medium low [22] medium high very_high very_high very_high medium very_high [29] low low low medium very_high low very_high [36] low very_high low low high medium medium [43] medium low low low low medium medium [50] medium Levels: low medium high very_high > > # step 4:繪制AAA_factor > plot(AAA_factor) >
2
(1)-1:刪除第一位的元素,-3:刪除第三位的元素
(2)
> credit_factor [1] BB AAA AA CCC AA AAA B BB Levels: AA AAA B BB CCC > # 刪除位于`credit_factor`第3和第7位的`A`級債券,不使用`drop=TRUE` > keep_level <- credit_factor[c(-3,-7)] > > # 繪制keep_level > plot(keep_level) > > # 使用相同的數據,刪除位于`credit_factor`第3和第7位的`A`級債券,使用`drop=TRUE` > drop_level <-credit_factor[c(-3,-7),drop=TRUE] > > # 繪制drop_level > plot(drop_level) >
>cash=data.frame(company = c("A", "A", "B"), cash_flow = c(100, 200, 300), year = c(1, 3, 2)) #創建數據框 >str(cash) 'data.frame': 3 obs. of 3 variables: $ company : Factor w/ 2 levels "A","B": 1 1 2 $ cash_flow: num 100 200 300 $ year : num 1 3 2
注意:創建數據框時,R的默認行為是將所有字符轉換為因子
那么,如何在創建數據框時,不讓r的默認行為執行呢?
采用 stringsAsFactors = FALSE
> cash=data.frame(company = c("A", "A", "B"), cash_flow = c(100, 200, 300), year = c(1, 3, 2),stringsAsFactors=FALSE) #創建數據框 > str(cash) 'data.frame': 3 obs. of 3 variables: $ company : chr "A" "A" "B" $ cash_flow: num 100 200 300 $ year : num 1 3 2
# 有序Factor類型 credit_rating <- c("AAA", "AA", "A", "BBB", "AA", "BBB", "A") credit_factor_ordered <- factor(credit_rating, ordered = TRUE, levels = c("AAA", "AA", "A", "BBB"))
>credit_rating <- c("BB", "AAA", "AA", "CCC", "AA", "AAA", "B", "BB") > credit_factor <- factor(credit_rating) # step 2.將credit_rating轉化為因子 > credit_factor #此時的credit_factor 無序 >ordered(credit_factor, levels = c("AAA", "AA", "A", "BBB"))
>credit_factor [1] AAA AA A BBB AA BBB A Levels: BBB < A < AA < AAA >credit_factor[-1] [1] AA A BBB AA BBB A Levels: BBB < A < AA < AAA #可見,AAA還存在 >credit_factor[-1, drop = TRUE] #完全放棄AAA級別 [1] AA A BBB AA BBB A Levels: BBB < A < AA
感謝各位的閱讀!關于“R語言中Factor類型變量怎么用”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。