R Factors


Factors

Factors are used to categorize data. Examples of factors are:

  • Demography: Male/Female
  • Music: Rock, Pop, Classic, Jazz
  • Training: Strength, Stamina

To create an element, use the factor() function and add a vector as an argument:


Example
music_genre <- factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Jazz"))

levels(music_genre)

Result
[1] "Classic" "Jazz"    "Pop"     "Rock"   
    

You can see from the example above that the feature has four levels (categories): Classic, Jazz, Pop and Rock.

To print only levels, use the levels() function:


Example
music_genre <- factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Jazz"), levels = c("Classic", "Jazz", "Pop", "Rock", "Other"))

levels(music_genre)

Result
[1] "Classic" "Jazz"    "Pop"     "Rock"    "Other"
    

You can also set levels, by adding a levels conflict within the function factor():


Example
music_genre <- factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Jazz"))

length(music_genre)

Result
[1] 8
    


Factor Length

Use the length() function to find out how many items are in feature:


Example
music_genre <- factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Jazz"))

length(music_genre)

Result
[1] 8
    


Access Factors

To access items for an item, see the reference number, using [] brackets:


Example
music_genre <- factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Jazz"))

music_genre[3]

Result
[1] Classic
Levels: Classic Jazz Pop Rock


Change Item Value

To change the value of an item, see the reference number:


Example

Change the value of the third item:

music_genre <- factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Jazz"))

music_genre[3] <- "Pop"

music_genre[3]

Result
[1] Pop
    Levels: Classic Jazz Pop Rock
    

Note that you cannot change the value of an item if it is not specified in the toolbar. The following example will illustrate an error:


Example

Trying to change the value of the third item ("Classic") to an item that does not exist/not predefined ("Opera"):

music_genre <- factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Jazz"))

music_genre[3] <- "Opera"

music_genre[3]

Result
Warning message:
    In `[<-.factor`(`*tmp*`, 3, value = "Opera") :
      invalid factor level, NA generated
    

However, if you have already specified it within the Level Conflict, it will apply:



Example

Change the value if the third item:

music_genre <- factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Jazz"), levels = c("Classic", "Jazz", "Pop", "Rock", "Opera"))

music_genre[3] <- "Opera"

music_genre[3]

Result
[1] Opera
    Levels: Classic Jazz Pop Rock Opera