📅  最后修改于: 2021-01-08 09:45:37             🧑  作者: Mango
该因子是一种数据结构,用于仅采用预定义的有限数量的值的字段。这些是采用有限数量的不同值的变量。这些是数据对象,用于对数据进行分类并将其存储在多个级别上。它既可以存储整数值也可以存储字符串值,并且在唯一值数量有限的列中很有用。
因子具有与存储在其中的唯一整数关联的标签。它包含称为水平的预定义设置值,默认情况下,R始终按字母顺序对水平排序。
R中有一个因子的以下属性
在R中,创建因子非常简单。分两个步骤创建因子
R提供factor()函数以将向量转换为factor。 factor()函数具有以下语法
factor_data<- factor(vector)
让我们看一个示例,以了解如何使用因子函数。
例
# Creating a vector as input.
data <- c("Shubham","Nishka","Arpita","Nishka","Shubham","Sumit","Nishka","Shubham","Sumit","Arpita","Sumit")
print(data)
print(is.factor(data))
# Applying the factor function.
factor_data<- factor(data)
print(factor_data)
print(is.factor(factor_data))
输出量
[1] "Shubham" "Nishka" "Arpita" "Nishka" "Shubham" "Sumit" "Nishka"
[8] "Shubham" "Sumit" "Arpita" "Sumit"
[1] FALSE
[1] Shubham Nishka Arpita Nishka Shubham Sumit Nishka Shubham Sumit
[10] Arpita Sumit
Levels: Arpita Nishka Shubham Sumit
[1] TRUE
像向量一样,我们可以访问因子的组成部分。访问因子分量的过程与向量更相似。我们可以借助索引方法或使用逻辑向量来访问元素。让我们看一个示例,在该示例中我们了解访问组件的不同方式。
例
# Creating a vector as input.
data <- c("Shubham","Nishka","Arpita","Nishka","Shubham","Sumit","Nishka","Shubham","Sumit","Arpita","Sumit")
# Applying the factor function.
factor_data<- factor(data)
#Printing all elements of factor
print(factor_data)
#Accessing 4th element of factor
print(factor_data[4])
#Accessing 5th and 7th element
print(factor_data[c(5,7)])
#Accessing all elemcent except 4th one
print(factor_data[-4])
#Accessing elements using logical vector
print(factor_data[c(TRUE,FALSE,FALSE,FALSE,TRUE,TRUE,TRUE,FALSE,FALSE,FALSE,TRUE)])
输出量
[1] Shubham Nishka Arpita Nishka Shubham Sumit Nishka Shubham Sumit
[10] Arpita Sumit
Levels: Arpita Nishka Shubham Sumit
[1] Nishka
Levels: Arpita Nishka Shubham Sumit
[1] Shubham Nishka
Levels: Arpita Nishka Shubham Sumit
[1] Shubham Nishka Arpita Shubham Sumit Nishka Shubham Sumit Arpita
[10] Sumit
Levels: Arpita Nishka Shubham Sumit
[1] Shubham Shubham Sumit Nishka Sumit
Levels: Arpita Nishka Shubham Sumit
像数据框一样,R允许我们修改因子。我们可以通过简单地重新分配因子来修改它的值。在R中,我们不能选择超出其预定义级别的值,这意味着如果其级别不存在,则无法插入值。为此,我们必须创建该值的级别,然后将其添加到我们的因子中。
让我们看一个示例,以了解如何在因素中进行修改。
例
# Creating a vector as input.
data <- c("Shubham","Nishka","Arpita","Nishka","Shubham")
# Applying the factor function.
factor_data<- factor(data)
#Printing all elements of factor
print(factor_data)
#Change 4th element of factor with sumit
factor_data[4] <-"Arpita"
print(factor_data)
#change 4th element of factor with "Gunjan"
factor_data[4] <- "Gunjan" # cannot assign values outside levels
print(factor_data)
#Adding the value to the level
levels(factor_data) <- c(levels(factor_data),"Gunjan")#Adding new level
factor_data[4] <- "Gunjan"
print(factor_data)
输出量
[1] Shubham Nishka Arpita Nishka Shubham
Levels: Arpita Nishka Shubham
[1] Shubham Nishka Arpita Arpita Shubham
Levels: Arpita Nishka Shubham
Warning message:
In `[<-.factor`(`*tmp*`, 4, value = "Gunjan") :
invalid factor level, NA generated
[1] Shubham Nishka Arpita Shubham
Levels: Arpita Nishka Shubham
[1] Shubham Nishka Arpita Gunjan Shubham
Levels: Arpita Nishka Shubham Gunjan
当我们创建带有一列文本数据的框架时,R将此文本列视为分类数据并在其上创建因子。
例
# Creating the vectors for data frame.
height <- c(132,162,152,166,139,147,122)
weight <- c(40,49,48,40,67,52,53)
gender <- c("male","male","female","female","male","female","male")
# Creating the data frame.
input_data<- data.frame(height,weight,gender)
print(input_data)
# Testing if the gender column is a factor.
print(is.factor(input_data$gender))
# Printing the gender column to see the levels.
print(input_data$gender)
输出量
height weight gender
1 132 40 male
2 162 49 male
3 152 48 female
4 166 40 female
5 139 67 male
6 147 52 female
7 122 53 male
[1] TRUE
[1] male male female female male female male
Levels: female male
在R中,我们可以借助因子函数来更改因子中水平的顺序。
例
data <- c("Nishka","Gunjan","Shubham","Arpita","Arpita","Sumit","Gunjan","Shubham")
# Creating the factors
factor_data<- factor(data)
print(factor_data)
# Apply the factor function with the required order of the level.
new_order_factor<- factor(factor_data,levels = c("Gunjan","Nishka","Arpita","Shubham","Sumit"))
print(new_order_factor)
输出量
[1] Nishka Gunjan Shubham Arpita Arpita Sumit Gunjan Shubham
Levels: Arpita Gunjan Nishka Shubham Sumit
[1] Nishka Gunjan Shubham Arpita Arpita Sumit Gunjan Shubham
Levels: Gunjan Nishka Arpita Shubham Sumit
R提供gl()函数来生成因子水平。此函数采用三个参数,即n,k和标签。在这里,n和k是整数,表示我们想要多少个水平以及每个水平需要多少次。
gl()函数的语法如下:
gl(n, k, labels)
例
gen_factor<- gl(3,5,labels=c("BCA","MCA","B.Tech"))
gen_factor
输出量
[1] BCA BCA BCA BCA BCA MCA MCA MCA MCA MCA
[11] B.Tech B.Tech B.Tech B.Tech B.Tech
Levels: BCA MCA B.Tech