文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

R语言 Factor类型的变量使用说明

2024-04-02 19:55

关注

factor类型的创建

1. 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

2. levels( )

上述代码中第二个运行后得到了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

3. Factor 汇总:summary()函数


> 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( )函数 对数据进行分组


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

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)
> 
> # 使用相同的数据,删除位于`credit_factor`第3和第7位的`A`级债券,使用`drop=TRUE`
> drop_level <-credit_factor[c(-3,-7),drop=TRUE]
> 
> # 绘制drop_level
> plot(drop_level)
> 

7. 转换Factor为String类型


>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

8. 创建有序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转化为因子
> 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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。如有错误或未考虑完全的地方,望不吝赐教。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯