欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

R語言 Factor類型的變量使用說明

 更新時間:2021年03月27日 11:46:40   作者:葑歆  
這篇文章主要介紹了R語言 Factor類型的變量使用說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

factor類型的創(chuàng)建

1. factor( )

> credit_rating <- c("BB", "AAA", "AA", "CCC", "AA", "AAA", "B", "BB") #生成名為credit_rating的字符向量
> credit_factor <- factor(credit_rating) # step 2.將credit_rating轉(zhuǎn)化為因子
> credit_factor
[1] BB AAA AA CCC AA AAA B BB 
Levels: AA AAA B BB CCC
> str(credit_rating) #調(diào)用str()函數(shù),顯示credit_rating結(jié)構(gòu)
 chr [1:8] "BB" "AAA" "AA" "CCC" "AA" "AAA" "B" "BB"
> str(credit_factor) #調(diào)用str()函數(shù),顯示credit_factor結(jié)構(gòu)
 Factor w/ 5 levels "AA","AAA","B",..: 4 2 1 5 1 2 3 4

2. levels( )

上述代碼中第二個運行后得到了levals,用于顯示不同的因子(不重復),上述代碼運行一二行

>credit_rating <- c("BB", "AAA", "AA", "CCC", "AA", "AAA", "B", "BB") 
> credit_factor <- factor(credit_rating) # step 2.將credit_rating轉(zhuǎn)化為因子
> 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

3. Factor 匯總:summary()函數(shù)

> summary(credit_rating)
 Length Class Mode 
 8 character character 
> summary(credit_factor)
 AA AAA B BB CCC 
 2 2 1 2 1 

4. factor 可視化:plot()

# 使用plot()將credit_factor可視化
plot(credit_factor)
#> summary(credit_factor)
# AA AAA B BB CCC 
 # 2 2 1 2 1 

5. cut( )函數(shù) 對數(shù)據(jù)進行分組

>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()函數(shù)為AAA_rank創(chuàng)建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()按順序?qū)⒓墑e重命名
> 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)
> 

6. 刪除元素 :- 表示刪除

(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)
> 
> # 使用相同的數(shù)據(jù),刪除位于`credit_factor`第3和第7位的`A`級債券,使用`drop=TRUE`
> drop_level <-credit_factor[c(-3,-7),drop=TRUE]
> 
> # 繪制drop_level
> plot(drop_level)
> 

7. 轉(zhuǎn)換Factor為String類型

>cash=data.frame(company = c("A", "A", "B"), cash_flow = c(100, 200, 300), year = c(1, 3, 2)) #創(chuàng)建數(shù)據(jù)框
>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

注意:創(chuàng)建數(shù)據(jù)框時,R的默認行為是將所有字符轉(zhuǎn)換為因子

那么,如何在創(chuàng)建數(shù)據(jù)框時,不讓r的默認行為執(zhí)行呢?

采用 stringsAsFactors = FALSE

> cash=data.frame(company = c("A", "A", "B"), cash_flow = c(100, 200, 300), year = c(1, 3, 2),stringsAsFactors=FALSE) #創(chuàng)建數(shù)據(jù)框
> 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

8. 創(chuàng)建有序Factor類型:ordered=TRUE

# 有序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轉(zhuǎn)化為因子
> credit_factor #此時的credit_factor 無序
>ordered(credit_factor, levels = c("AAA", "AA", "A", "BBB"))

9. 刪除因子級別時,采用drop=TRUE

>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

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • R語言多元線性回歸實例詳解

    R語言多元線性回歸實例詳解

    對比一元線性回歸,多元線性回歸是用來確定2個或2個以上變量間關(guān)系的統(tǒng)計分析方法,下面這篇文章主要給大家介紹了關(guān)于R語言多元線性回歸的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-07-07
  • R語言 實現(xiàn)將factor轉(zhuǎn)換成numeric方法

    R語言 實現(xiàn)將factor轉(zhuǎn)換成numeric方法

    這篇文章主要介紹了R語言 實現(xiàn)將factor轉(zhuǎn)換成numeric方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • R語言-使用快捷鍵快速注釋的實現(xiàn)

    R語言-使用快捷鍵快速注釋的實現(xiàn)

    這篇文章主要介紹了R語言-使用快捷鍵快速注釋的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • R語言數(shù)據(jù)可視化繪圖bar chart條形圖實現(xiàn)示例

    R語言數(shù)據(jù)可視化繪圖bar chart條形圖實現(xiàn)示例

    這篇文章主要為大家介紹了R語言數(shù)據(jù)可視化繪圖bar chart條形圖的實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2022-02-02
  • R語言中平均值、中位數(shù)和模式知識點總結(jié)

    R語言中平均值、中位數(shù)和模式知識點總結(jié)

    在本篇文章里小編給大家整理的是一篇關(guān)于R語言中平均值、中位數(shù)和模式知識點總結(jié)內(nèi)容,有興趣的朋友們跟著學習下。
    2021-05-05
  • R語言histogram(直方圖)的具體使用

    R語言histogram(直方圖)的具體使用

    這篇文章主要介紹了R語言histogram(直方圖)的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • R語言繪制數(shù)據(jù)可視化小提琴圖Violin plot with dot畫法

    R語言繪制數(shù)據(jù)可視化小提琴圖Violin plot with dot畫法

    這篇文章主要為大家介紹了R語言繪制數(shù)據(jù)可視化小提琴圖Violin plot with dot畫法的示例詳解有需要的朋友可以借鑒參考下希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-02-02
  • 解決R語言報錯:Error?in?y?+?1:non-numeric?argument?to?binary?operator

    解決R語言報錯:Error?in?y?+?1:non-numeric?argument?to?binary

    R語言編程中的常見錯誤有一些錯誤是R的初學者和經(jīng)驗豐富的R程序員都可能常犯的,下面這篇文章主要給大家介紹了關(guān)于解決R語言報錯:Error?in?y?+?1:non-numeric?argument?to?binary?operator的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • 詳解R語言caret包trainControl函數(shù)

    詳解R語言caret包trainControl函數(shù)

    這篇文章主要介紹了R語言caret包trainControl函數(shù)詳解,本文通過源碼分析給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-08-08
  • R語言可視化存儲矢量圖實現(xiàn)方式

    R語言可視化存儲矢量圖實現(xiàn)方式

    這篇文章主要為大家介紹了R語言存儲矢量圖的實現(xiàn)方式過程,有需要的朋友可以借鑒參考下,希望能夠有所你幫助,祝大家多多進步,早日升職加薪
    2021-11-11

最新評論