Sure! Data types in R represent the different kinds of values that variables can hold. Here's a simple explanation with examples:
Numeric data type represents numbers, including integers and decimals.
Example
# Integer
x <- 10
# Decimal (floating-point number)
y <- 3.14
Character data type represents text or strings.
Example
name <- "Alice"
Logical data type represents boolean values - either TRUE
or FALSE
.
Example
is_valid <- TRUE
is_active <- FALSE
Integer data type specifically represents whole numbers.
Example
count <- 5L
Complex data type represents complex numbers with real and imaginary parts.
Example
z <- 3 + 4i
You can check the data type of a variable using the class()
function
Example
class(x) # This will return "numeric"
class(name) # This will return "character"
class(is_valid) # This will return "logical"
class(count) # This will return "integer"
class(z) # This will return "complex"
Understanding data types is essential for working with data in R. Each data type has its own characteristics and operations that can be performed on it. By knowing the data type of your variables, you can write more effective and efficient code.