Data Types or Data Structures in R Programming: Scalars, Vectors (numerical, character, logical), Matrices , Data frames and Lists

__This article is part of R-Basics learning materials:__

Data types play a crucial role in programming languages as they define the kind of data that can be stored and manipulated. In R programming, various data types are available, each serving a specific purpose. This article provides an overview of the fundamental data types in R and demonstrates how to determine the type of a variable using the class function.

R Data Types:

### Scalars

Scalars represent single values in R. They include numeric scalars (e.g., 4.5), integer scalars (e.g., 4), logical scalars (TRUE or FALSE), and character scalars (text enclosed in quotes, e.g., "R is fantastic").

### Vectors

Vectors are one-dimensional arrays that can contain multiple values of the same data type. R supports numerical vectors, character vectors, and logical vectors. Numerical vectors hold numeric values, character vectors store text data, and logical vectors contain logical (Boolean) values.

### Matrices

Matrices are two-dimensional data structures with rows and columns. They can be thought of as a collection of vectors of the same length. Matrices are often used for mathematical computations and data analysis.

### Data frames

Data frames are similar to matrices but can hold different types of data. They are tabular structures with rows and columns, where each column can have a different data type. Data frames are commonly used to represent datasets, with each column representing a variable and each row representing an observation.

### Lists

Lists are versatile data structures that can hold elements of different types. They can contain vectors, matrices, data frames, or even other lists. Lists provide flexibility and are useful when dealing with complex data structures.

### Examples

**Example 1**

```
# Numeric
age <- 28
class(age)
```

Output:

`[1] "numeric"`

**Example 2**

```
# Character
message <- "R is amazing"
class(message)
```

Output:

`[1] "character"`

**Example 3**

```
# Logical
is_r_fun <- TRUE
class(is_r_fun)
```

Output:

`[1] "logical"`

Understanding data types is essential for effective programming in R. Scalar values, vectors, matrices, data frames, and lists offer different ways to structure and manipulate data in R. By utilizing the class function, you can easily determine the data type of a variable, ensuring proper data handling and analysis in your R programs.