在R编程中将向量转换为因子– as.factor()函数

2021年3月19日18:37:16 发表评论 2,789 次浏览

as.factor()发挥作用R语言用于将传递的对象(通常为Vector)转换为因子。

语法:as.factor(object)参数:Object:要转换的向量

范例1:

# Creating a vector 
x< - c( "female" , "male" , "male" , "female" ) 
  
# Using as.factor() Function
# to convert vector into factor
as.factor(x)

输出如下:

[1] female male   male   female
Levels: female male

范例2:

# Creating a vector 
x< - c( "North" , "North" , "East" , "West" ) 
  
# Using as.factor() Function
# to convert vector into factor
as.factor(x)

输出如下:

[1] North North East  West 
Levels: East North West

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: