Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

R 因子


因子

因子用于对数据进行分类。因子的示例包括:

  • 人口统计:男性/女性
  • 音乐:摇滚、流行、古典、爵士
  • 训练:力量、耐力

要创建因子,可以使用 factor() 函数并添加向量作为参数。

示例

# 创建一个因子
music_genre <- factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Jazz"))

# 打印因子
music_genre

结果

[1] Jazz    Rock    Classic Classic Pop     Jazz    Rock    Jazz
Levels: Classic Jazz Pop Rock
自己尝试 »

从上面的示例中可以看出,该因子有四个水平(类别):古典、爵士、流行和摇滚。

要仅打印水平,可以使用 levels() 函数。

示例

music_genre <- factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Jazz"))

levels(music_genre)

结果

[1] "Classic" "Jazz"    "Pop"     "Rock"   
自己尝试 »

您还可以设置水平,方法是在 factor() 函数内部添加 levels 参数。

示例

music_genre <- factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Jazz"), levels = c("Classic", "Jazz", "Pop", "Rock", "Other"))

levels(music_genre)

结果

[1] "Classic" "Jazz"    "Pop"     "Rock"    "Other"
自己尝试 »

因子长度

使用 length() 函数找出因子中包含多少个项目。

示例

music_genre <- factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Jazz"))

length(music_genre)

结果

[1] 8
自己尝试 »


访问因子

要访问因子中的项目,请使用 [] 括号引用索引号。

示例

访问第三个项目

music_genre <- factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Jazz"))

music_genre[3]

结果

[1] Classic
Levels: Classic Jazz Pop Rock
自己尝试 »

更改项目值

要更改特定项目的价值,请参考索引号。

示例

更改第三个项目的价值

music_genre <- factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Jazz"))

music_genre[3] <- "Pop"

music_genre[3]

结果

[1] Pop
Levels: Classic Jazz Pop Rock
自己尝试 »

请注意,如果您未在因子中指定特定项目的价值,则无法更改其价值。以下示例将产生错误。

示例

尝试将第三个项目(“古典”)的价值更改为不存在/未预定义的项目(“歌剧”)。

music_genre <- factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Jazz"))

music_genre[3] <- "Opera"

music_genre[3]

结果

Warning message:
In `[<-.factor`(`*tmp*`, 3, value = "Opera") :
  invalid factor level, NA generated
自己尝试 »

但是,如果您已经在 levels 参数内指定了它,它将起作用。

示例

更改第三个项目的价值

music_genre <- factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Jazz"), levels = c("Classic", "Jazz", "Pop", "Rock", "Opera"))

music_genre[3] <- "Opera"

music_genre[3]

结果

[1] Opera
Levels: Classic Jazz Pop Rock Opera
自己尝试 »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.