May 12, 2021 R language tutorial
A factor is a data object that is used to classify data and store it as a level. T
hey can store strings and integers. T
hey are useful in columns with a limited number of unique values. L
ike "male," "female" and True, False, etc.
They are useful in statistical modeling data analysis.
Use
the factor() function
to create a factor by using the amount that will be used as input.
# Create a vector as input. data <- c("East","West","East","North","North","East","West","West","West","East","North") print(data) print(is.factor(data)) # Apply the factor function. factor_data <- factor(data) print(factor_data) print(is.factor(factor_data))
When we execute the code above, it produces the following results -
[1] "East" "West" "East" "North" "North" "East" "West" "West" "West" "East" "North" [1] FALSE [1] East West East North North East West West West East North Levels: East North West [1] TRUE
When you create any data frame that has a text data column, the R language treats the text column as classified data and creates factors on it.
# Create the vectors for data frame. height <- c(132,151,162,139,166,147,122) weight <- c(48,49,66,53,67,52,40) gender <- c("male","male","female","female","male","female","male") # Create the data frame. input_data <- data.frame(height,weight,gender) print(input_data) # Test if the gender column is a factor. print(is.factor(input_data$gender)) # Print the gender column so see the levels. print(input_data$gender)
When we execute the code above, it produces the following results -
height weight gender 1 132 48 male 2 151 49 male 3 162 66 female 4 139 53 female 5 166 67 male 6 147 52 female 7 122 40 male [1] TRUE [1] male male female female male female male Levels: female male
You can change the order of levels in a factor by re-applying the factor function with the new hierarchy.
data <- c("East","West","East","North","North","East","West","West","West","East","North") # Create the factors factor_data <- factor(data) print(factor_data) # Apply the factor function with required order of the level. new_order_data <- factor(factor_data,levels = c("East","West","North")) print(new_order_data)
When we execute the code above, it produces the following results -
[1] East West East North North East West West West East North Levels: East North West [1] East West East North North East West West West East North Levels: East West North
We can use the gl() function to generate factor levels. I t requires two integers as inputs, indicating how many levels and how many times each level is.
gl(n, k, labels)
The following is a description of the parameters used -
n is an integer that gives a series.
k is an integer that gives the number of copies.
Labels are label vectors for the resulting factor level.
v <- gl(3, 4, labels = c("Tampa", "Seattle","Boston")) print(v)
When we execute the code above, it produces the following results -
Tampa Tampa Tampa Tampa Seattle Seattle Seattle Seattle Boston [10] Boston Boston Boston Levels: Tampa Seattle Boston