Frequency Table in R: Factors Variable

Recall that in R language a factor is a variable that defines a partition into groups. A single factor variable can be used to create a simple frequency table in R, while a pair of factors can be used to define a two-way cross-classification (contingency or frequency distribution). For this purpose, the table() function allows to creation of frequency tables. The frequency table is calculated from equal length factors.

Frequency Table in R of Categorical/ Group/ Factor Variable

We will use the “mtcars” dataset. For the variable $gear$, let us create a frequency table using the table() function. The table() function will count the gear code for each entry in the data vector. For example,

attach(mtcars)

freq <- table(gear)
freq
frequency table using factor

The freq object will give a table of frequencies of each gear code in the sample. It is important to note that, the frequencies are ordered and labeled by the levels attribute of the factor.

Frequency Distribution of a Continuous Variable

One can also create a frequency distribution table for a continuous variable. Suppose from the mtcars data set, we are interested in creating a frequency table of $mpg$ variable. For this purpose, first, we need to define the cut points or bins to define the classes/groups of the frequency table. For example,

cut(mpg, 10+5*(0:5))

## Output
(10,15] (15,20] (20,25] (25,30] (30,35] 
      6      12       8       2       4 

The cut() function is used to split the continuous data vector into groups. The groups are defined by creating a sequence of values using 10+5*(0:5), that is

10+5*(0:5)

## Output
10 15 20 25 30 35

The cut() function, cuts and counts the occurrence of each observation of mpg regarding the cut points created using breaks = 10+5*(0:5). The frequency table will be

frequency table of a continuous variable

Creating Graph of Frequency Table

For the frequency table created above, one can easily create different graphical representations, such as pie charts and bar plots of the frequency table. For example,

freq<-table(cut(mpg, 10+5*(0:5)))
pie(freq)
hist(freq)
barplot(freq)
plot(freq)
Bar plot frequency table in R
bar plot in R language
pie chart in frequency table in R language

Note that: for a $k$ factor argument, the result is a $k$-way array of frequencies.

https://itfeature.com, https://gmstat.com

Types of Objects in R

R language operates on entities which are known as objects. There are various types of objects in R exists, such as vectors, matrices, factors, lists, data frames, functions, etc. In R, objects are classified into several types based on their structure and content.

Types of Objects in R

Matrices

Arrays or matrices are multi-dimensional generalizations of vectors. Matrices are vectors indexed by two or more indices and displayed specially. Matrices contain rows and columns of homogeneous elements. The class of matrices object is “matrix”. See more about matrices by following the matrices.

Factors

Factors are used to handle categorical data. Factor variables may contain two or more levels, used to define the group or category of the variable. See more about factors in detail by following factors.

Lists

Lists are a general form of vectors in which the various elements need not be of the same type, that is, lists may contain heterogeneous data. Lists are often vectors or lists themselves. Lists are a convenient way to get different results from statistical computation, as lists may contain different types of data objects. See more about lists by following the link Lists.

Data Frame

Data frame objects are similar to matrix object structures. Unlike matrix objects, the data frame objects may contain different types of objects, that is, heterogeneous data. Think of the data frame as “Data Matrices” with one row per observational unit but with (possibly) both numerical and categorical variables. Many experiments are best described by data frames, the treatments are categorical but the response (output) is numeric. For more details about the data frame, follow the link data frame.

Functions

Functions are themselves objects. In R Language, functions can be stored in the project’s workspace. Functions provide a quick, simple, and convenient way to extend the functionality and power of R. See more about functions and customization of functions, see Functions.

Examples of Different Types of Objects in R

# Scalar types
x <- 5        # Numeric (integer)
y <- 3.14159  # Numeric (double)
z <- "Hello"  # Character
b <- TRUE     # Logical

# Vector types
numbers <- c(1, 2, 3, 4)                  # Numeric vector
fruits <- c("apple", "banana", "orange")  # Character vector
bools <- c(TRUE, FALSE, TRUE)             # Logical vector

# Data frame
df <- data.frame(
  name = c("Ali", "Babar", "Usman"),
  age = c(25, 30, 28),
  city = c("Multan", "Lahore", "Karachi")
)

# Matrix
mat <- matrix(1:9, nrow = 3, ncol = 3)

# List
my_list <- list(
  numbers = numbers,
  fruits = fruits,
  df = df
)

# Factor
colors <- factor(c("red", "blue", "green", "red"))
Types of Objects in R Language

https://itfeature.com, https://gmstat.com

Logical Vectors in R: A Quick Guide

The logical vectors in R Language are the vectors whose elements are TRUE, FALSE, or NA (Not Available). R language allows the easy manipulation of logical (or relational) quantities. The TRUE and FALSE values are often used to represent the conditions or Boolean expressions.

In R, the reserved words TRUE and FALSE are often abbreviated as T and F, respectively. However, the T and F are not reserved words and hence can be overwritten by the user. Therefore, instead of T and F; it is better to use TRUE and FALSE.

Logical vectors in R can be created by:

  • Direct assignment of TRUE and FALSE values to the elements of a vector
  • By using conditions (use of logical or comparison operators) on elements of the vectors. (Operators in R Language)
  • Using ifelse statement

Creating Logical Vectors in R Using Direct Assignment

v1 <- c(TRUE, FALSE, TRUE)
print(v1)
## Output
[1]  TRUE FALSE  TRUE

Creating Logical Vectors using Comparison Operators

x <- 5
y <- 10
v2 <- x > y
print(v2)
## Output
FALSE
Logical Vectors in R using Comparison Operators
data <- c(1, 2, 3, 4, 5)
v3 <- data < 3
print(v3)
## Output
[1]  TRUE  TRUE FALSE FALSE FALSE
Logical Vectors in R

Creating Logical Vectors using ifelse Statement

The ifelse statement can also be used to create/generate logical vectors in R Language. For example,

data <- c(3, 4, 6, 8, 4, 4, 6, 10, -5)
v4 <- ifelse(data > 5, TRUE, FALSE)
print(v4)

## Output
[1] FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE  TRUE FALSE

From the above examples, the logical vectors are usually generated by conditions. The length of the logical vector will be the same as that of the vectors to which the condition is applied. Depending on the condition, the corresponding elements result in FALSE if the element of the vectors does not meet the condition specified and TRUE where it is.

Logical Operators

The following is the list of logical operators

Logical OperatorShort Description
<Less than
>Greater than
<=Less than or Equal to
>=Greater than or Equal to
==Exactly Equal to
!=Not Equal to

In addition to logical operators, the relational/logical operators are:

OperatorShort Description
& (and)It takes two logical values and returns TRUE only if both values are TRUE themselves
| (or)It takes two logical values and returns TRUE if just one value is TRUE.
! (not)It negates the logical value it’s used on

Use of Logical Operators

Filtering Data

The logical vectors in R language are commonly used for filtering the data. For example,

data <- data.frame(x = c(1, 2, 3, 4, 5), y = c("a", "b", "c", "d", "e"))
filtered_data <- data[data$x > 3, ]
Logical Vectors in R: Filtering Data

Ordinary Arithmetic

Logical vectors may be used in ordinary arithmetic, in which case they are coerced into numeric vectors, FALSE becoming 0 and TRUE becoming. For example,

x = c(TRUE, FALSE, FALSE, TRUE)
y = c(5, 10, 6, 15)
x+y

## Output
[1]  6 10  6 16

sum(x)
## Output
[1] 2

Logical vectors in R language are a fundamental tool for working with conditions and Boolean expressions. Understanding how to create, manipulate, and use logical vectors is essential for effective data analysis and programming in R.

https://itfeature.com, https://gmstat.com