Introduction

Data types are an essential concept in any programming language. They define the kind of data that can be stored and manipulated within a program. In R, the primary data types are Numeric, Integer, Character, Logical, Complex, and Factor. This document provides an overview of each data type along with examples.

Numeric

The numeric data type represents real numbers (numbers with a decimal point). They are used to store both positive and negative numbers.

# Example of Numeric Data Type
numeric_example <- 42.5
class(numeric_example)  # This will return "numeric"
## [1] "numeric"

Numeric Example: 42.5

Integer

Integer data type represents whole numbers without a decimal point. In R, integers are explicitly defined by adding an L after the number.

# Example of Integer Data Type
integer_example <- 42L
class(integer_example)  # This will return "integer"
## [1] "integer"

Integer Example: 42L

Character

Character data type is used for text data, which includes letters, numbers, and symbols enclosed in quotes.

# Example of Character Data Type
character_example <- "Hello, World!"
class(character_example)  # This will return "character"
## [1] "character"

Character Example: “Hello, World!”

Logical

Logical data type represents boolean values, which can either be TRUE or FALSE.

# Example of Logical Data Type
logical_example <- TRUE
class(logical_example)  # This will return "logical"
## [1] "logical"

Logical Example: TRUE

Complex

Complex data type is used for complex numbers, which have a real and an imaginary part.

# Example of Complex Data Type
complex_example <- 3 + 2i
class(complex_example)  # This will return "complex"
## [1] "complex"

Complex Example: 3 + 2i

Factor

Factor data type is used for categorical data and can store both nominal and ordinal variables. Factors are useful when you have a fixed set of possible values.

# Example of Factor Data Type
factor_example <- factor(c("High", "Medium", "Low"))
class(factor_example)  # This will return "factor"
## [1] "factor"

Factor Example: factor(c(“High”, “Medium”, “Low”))

Conclusion

Understanding data types is crucial in R programming as they determine the operations that can be performed on data and the type of output expected. Correctly identifying and using data types ensures that your code runs efficiently and produces accurate results.