Factors in Data Frame
The Data frame is similar to a 2D array with the columns containing all the values of one variable and the rows having one set of values from every column. There are four things to remember about data frames:
- column names are compulsory and cannot be empty.
- Unique names should be assigned to each row.
- The data frame’s data can be only of three types- factor, numeric, and character type.
- The same number of data items must be present in each column.
In R language when we create a data frame, its column is categorical data, and hence a R factor is automatically created on it.
We can create a data frame and check if its column is a factor.
Example
R
age <- c (40, 49, 48, 40, 67, 52, 53) salary <- c (103200, 106200, 150200, 10606, 10390, 14070, 10220) gender <- c ( "male" , "male" , "transgender" , "female" , "male" , "female" , "transgender" ) employee<- data.frame (age, salary, gender) print (employee) print ( is.factor (employee$gender)) |
Output
age salary gender 1 40 103200 male 2 49 106200 male 3 48 150200 transgender 4 40 10606 female 5 67 10390 male 6 52 14070 female 7 53 10220 transgender [1] TRUE
R Factors
Factors in R Programming Language are data structures that are implemented to categorize the data or represent categorical data and store it on multiple levels.
They can be stored as integers with a corresponding label to every unique integer. The R factors may look similar to character vectors, they are integers and care must be taken while using them as strings. The R factor accepts only a restricted number of distinct values. For example, a data field such as gender may contain values only from female, male, or transgender.
In the above example, all the possible cases are known beforehand and are predefined. These distinct values are known as levels. After a factor is created it only consists of levels that are by default sorted alphabetically.
Contact Us