# blog posts # Invoices; They are a type of data object in the R programming language that are used to classify data and store it as a surface. These factors can store both strings and integers.

R programming language, They are useful for columns that have a limited number of specific values. Such as “Male”, “Female”, True, False, etc. They are useful in data analysis for statistical modeling.

## Example

# 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 run the above code; The following result is obtained:

 “East” “West” “East” “North” “North” “East” “West” “West” “West” “East” “North”

 FALSE

 East West East North North East East West West West East

Levels: East North West

 TRUE

## Invoices in the data frame

In creating any data frame with a column of textual data, R treats the textual column as an absolute data and generates 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 run the above code; The following results are obtained:

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

 TRUE

 male male female female male female male

Levels: female male

## Change the order of the levels

The order of the levels in an invoice can be determined by re-applying the invoice function to a new order of levels; Changed.

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)

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 run the above code; The following result is obtained:

 East West East North North East East West West West East

Levels: East North West

 East West East North North East East West West West East

Levels: East West North

## Generate levels in one invoice

We can create invoice levels using the gl () function . This function takes two integers as input, which represent the number of levels and the number of repetitions of each level.

Syntax

gl (n, k, labels)

The parameters used in the above code are:

• n is an integer that indicates the number of levels.
• k is an integer that indicates the number of repetitions.
• lables is the removal of labels for invoice levels.

Example

v <- gl (3, 4, labels = c (“Tampa”, “Seattle”, “Boston”))

print (v)

When we run the above code; We get the following result:

Tampa Tampa Tampa Tampa Seattle Seattle Seattle Seattle Seattle Boston

 Boston Boston Boston

Levels: Tampa Seattle Boston