Data Types in R
R, like many other programming languages, has a variety of data types to represent different
kinds of data. Here are the primary data types in R:
1. Numeric Data Types:
● Numeric: Represents real numbers, including integers and decimal numbers.
x <- 10 # Integer
y <- 3.14 # Decimal number
2. Character Data Type:
● Character: Represents text strings.
name <- "Alice"
3. Logical Data Type:
● Logical: Represents Boolean values (TRUE or FALSE).
is_true <- TRUE
is_false <- FALSE
4. Complex Data Type:
● Complex: Represents complex numbers with real and imaginary parts.
z <- 2 + 3i
5. Special Data Types:
● Integer: Represents integer numbers.
● Factor: Represents categorical data.
● Date: Represents dates.
● POSIXct: Represents date and time.
Example:
# Creating a vector of different data types
my_vector <- c(10, "Hello", TRUE, 2+3i)
Data Structures in R:
R also provides several data structures to organize and store data:
● Vector: A one-dimensional array of elements of the same data type.
● Matrix: A two-dimensional array of elements of the same data type.
● Array: A multi-dimensional array of elements of the same data type.
● Data Frame: A tabular data structure with rows and columns, similar to a spreadsheet.
● List: A collection of objects of different data types.
By understanding these data types and data structures, you can effectively work with data in R
and perform various data analysis tasks.