📜  如何在 R 中重命名因子水平?

📅  最后修改于: 2022-05-13 01:55:51.301000             🧑  作者: Mango

如何在 R 中重命名因子水平?

在本文中,我们将介绍如何在 R 编程语言中重命名因子级别。

R中的因子变量使用分类变量表示,分类变量使用各种级别表示。每个唯一值都使用唯一级别值表示。可以使用 factor() 方法声明 R 中的因子变量或向量。

可以使用levels() 方法提取因子的唯一级别,该方法将创建的因子向量作为参数。它显示因子变量中的所有唯一条目。

方法 1:使用基本 R 方法

可以使用比较和索引运算符重命名因子级别。比较现有的因子值,然后通过将其分配给新值进行修改。对现有因子向量进行更改。遵循以下语法:

R
# declare a factor
val < - factor(c("Geeks", "For", "Geeks",
                 "Coding", "Fun"))
  
# printing the levels of factor variables
print("Levels of factor")
lvls < - levels(val)
print(lvls)
  
# renaming the factors
levels(val)[levels(val) == "Coding"] < - "Learning"
print("Modified Levels of factor")
lvls < - levels(val)
print(lvls)


R
# declare a factor 
val <- factor(c("Geeks","For","Geeks",
                "Coding","Fun"))
  
# printing the levels of factor variables
print("Levels of factor")
lvls <- levels(val)
print(lvls)
  
# renaming the factors
levels(val) <- list("Learning"="Fun",
                    "Programming"="Coding")
print("Modified Levels of factor")
print(levels(val))


R
# declare a factor 
val <- factor(c("Geeks","For","Geeks",
                "Coding","Fun"))
  
# printing the levels of factor variables
print("Levels of factor")
lvls <- levels(val)
print(lvls)
  
# renaming the factors
levels(val) <- c("Hi","There",
                 "Welcome","Here")
print("Modified Levels of factor")
print(levels(val))


R
# importing req libraries
library("plyr")
  
# declare a factor 
val <- factor(c("Geeks","For","Geeks",
                "Coding","Fun"))
  
# printing the levels of factor variables
print("Levels of factor")
lvls <- levels(val)
print(lvls)
  
# renaming the factors
revalue(val, c("Fun"= "Learning", "Coding"= "Programming"))
print("Modified Levels of factor")
print(levels(val))


R
# importing req libraries
library("plyr")
  
# declare a factor 
val <- factor(c("Geeks","For","Geeks",
                "Coding","Fun"))
  
# printing the levels of factor variables
print("Levels of factor")
lvls <- levels(val)
print(lvls)
  
# renaming the factors
mapvalues(val, from = c("Fun","Coding"),
          to = c("Learning", "Programming"))
print("Modified Levels of factor")
print(levels(val))


输出

[1] "Levels of factor"
[1] "Coding" "For"    "Fun"    "Geeks" 
[1] "Modified Levels of factor"
[1] "Learning" "For"      "Fun"      "Geeks"  

list() 方法还可用于创建要创建的因子级别列表,然后可以重新分配给新值。然后将levels() 重新分配给这些新声明的值。对原始因子向量进行更改。使用此方法可以重命名多个因子。

list(new-fac=val = old-fac-val,..)

在 list() 方法中声明了新旧因子变量值的组合。

R

# declare a factor 
val <- factor(c("Geeks","For","Geeks",
                "Coding","Fun"))
  
# printing the levels of factor variables
print("Levels of factor")
lvls <- levels(val)
print(lvls)
  
# renaming the factors
levels(val) <- list("Learning"="Fun",
                    "Programming"="Coding")
print("Modified Levels of factor")
print(levels(val))

输出:

[1] "Levels of factor"
[1] "Coding" "For"    "Fun"    "Geeks" 
[1] "Modified Levels of factor"
[1] "Learning"    "Programming"

因子变量的所有级别也可以使用 c() 方法重命名,以创建级别向量。然后将新创建的值分配给因子变量。以下代码片段说明了此过程:

R

# declare a factor 
val <- factor(c("Geeks","For","Geeks",
                "Coding","Fun"))
  
# printing the levels of factor variables
print("Levels of factor")
lvls <- levels(val)
print(lvls)
  
# renaming the factors
levels(val) <- c("Hi","There",
                 "Welcome","Here")
print("Modified Levels of factor")
print(levels(val))

输出:

[1] "Levels of factor"
[1] "Coding" "For"    "Fun"    "Geeks" 
[1] "Modified Levels of factor"
[1] "Hi"      "There"   "Welcome" "Here"  

方法二:使用 revalue() 方法

R 中的 plyr 包用于拆分数据,对其执行操作并将其重新组合在一起。可以使用以下命令将包下载并安装到工作空间中:

install.packages("plyr")

此包中的 revalue 方法用于在因子向量中用新定义的值替换指定值。在这种情况下,因子变量的水平不会被修改并保持原始状态。

R

# importing req libraries
library("plyr")
  
# declare a factor 
val <- factor(c("Geeks","For","Geeks",
                "Coding","Fun"))
  
# printing the levels of factor variables
print("Levels of factor")
lvls <- levels(val)
print(lvls)
  
# renaming the factors
revalue(val, c("Fun"= "Learning", "Coding"= "Programming"))
print("Modified Levels of factor")
print(levels(val))

输出:

[1] "Levels of factor"
[1] "Coding" "For"    "Fun"    "Geeks"  
Geeks For Geeks Programming Learning
Levels:
[1] "Modified Levels of factor"
[1] "Coding" "For"    "Fun"    "Geeks" 

方法 3:使用 mapvalues() 方法

R 中 plyr 包中的 mapvalues() 方法用于将因子向量中的指定值替换为新值。更改不会保留在原始向量中。

R

# importing req libraries
library("plyr")
  
# declare a factor 
val <- factor(c("Geeks","For","Geeks",
                "Coding","Fun"))
  
# printing the levels of factor variables
print("Levels of factor")
lvls <- levels(val)
print(lvls)
  
# renaming the factors
mapvalues(val, from = c("Fun","Coding"),
          to = c("Learning", "Programming"))
print("Modified Levels of factor")
print(levels(val))

输出:

[1] "Levels of factor"
[1] "Coding" "For"    "Fun"    "Geeks"  
Geeks For Geeks Programming Learning
Levels:
[1] "Modified Levels of factor"
[1] "Coding" "For"    "Fun"    "Geeks"