R 中的数据类型

Factors

The term factor refers to a statistical data type used to store categorical variables.

  • categorical variable
    • nominal categorical variable: a categorical variable without an implied order
    • ordinal categorical variable
  • continuous variable

Factor 类型的基本用法

# create a vector that contains all the observations that belong to a limited number of categories. 
sex_vector <- c("Male","Female","Female","Male","Male")

# function factor() will encode the vector as a factor:
factor_sex_vector <- factor(sex_vector)
# Animals
animals_vector <- c("Elephant", "Giraffe", "Donkey", "Horse")
factor_animals_vector <- factor(animals_vector)
> factor_animals_vector
[1] Elephant Giraffe  Donkey   Horse   
Levels: Donkey Elephant Giraffe Horse

# Temperature
temperature_vector <- c("High", "Low", "High","Low", "Medium")
factor_temperature_vector <- factor(temperature_vector, order = TRUE, levels = c("Low", "Medium", "High"))
> factor_temperature_vector
[1] High   Low    High   Low    Medium
Levels: Low < Medium < High

Factor 类型更改 levels

# Code to build factor_survey_vector
survey_vector <- c("M", "F", "F", "M", "M")
factor_survey_vector <- factor(survey_vector)

> levels(factor_survey_vector)
[1] "F" "M"


# Specify the levels of factor_survey_vector
levels(factor_survey_vector) <- c('Female', 'Male')

> factor_survey_vector
[1] Male   Female Female Male   Male  
Levels: Female Male

猜你喜欢

转载自blog.csdn.net/Guo_ya_nan/article/details/81113092