Factors in the R language

Source: Internet
Author: User

Factor (factor) and ordered factor (ordered factor)
Factors are used to store categorical variables (categorical variables) and ordered variables, which cannot be used to calculate but only to classify or count. Factors represent categorical variables, and ordered factors represent ordered variables.
The function that generates the factor data object is factor (), the syntax is factor (data, levels, labels, ...), where data is the parameter, levels is the factor horizontal vector, and labels is the label vector of the factor.
1. Create a factor.
Example 1:colour <-C (' G ', ' g ', ' r ', ' Y ', ' g ', ' y ', ' y ', ' r ', ' Y ')
Col <-factor (colour)
Col1 <-factor (colour, levels = C (' G ', ' R ', ' Y '), labels = c (' Green ', ' Red ', ' Yellow '))
#labels的内容替换colour相应位置对应levels的内容 col1

[1] Green green red Yellow green Yellow Yellow Red Yellow
Levels:green Red Yellow


Col2 <-factor (colour, levels = C (' G ', ' R ', ' Y '), labels = c (' 1 ', ' 2 ', ' 3 '))
Col_vec <-as.vector (col2) #转换成字符向量 "1" "1" "2" "3" "1" "3" "3" "2" "3"
Col_num <-as.numeric (col2) #转换成数字向量 1 1 2 3 1 3 3 2 3
Col3 <-factor (colour, levels = C (' G ', ' R '))
[1] G G r <NA> G <NA> <NA> R <NA>

Levels:g R


2. Create an ordered factor
Example 1:score <-C (' A ', ' B ', ' A ', ' C ', ' B ')
Score1 <-ordered (score, levels = C (' C ', ' B ', ' A ')); Score1

[1] A B a C b
Levels:c < B < A



3. Use the Cut () function to convert the general data to the cause or order factor.
Example 1:exam <-C (98, 97, 52, 88, 85, 75, 97, 92, 77, 74, 70, 63, 97, 71, 98,
65, 79, 74, 58, 59, 60, 63, 87, 82, 95, 75, 79, 96, 50, 88)
EXAM1 <-cut (exam, breaks = 3) #切分成3组
EXAM2 <-cut (exam, breaks = C (0, D, D, D,)) #切分成自己设置的组
attr (exam1, ' levels '); attr (exam2, ' levels '); attr (exam2, ' class ')
Ordered (exam2, labels = c (' bad ', ' OK ', ' average ', ' good ', ' excellent ')) #一个有序因子

Factors in the R language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.