Data Types in R
Ibrahim Rapemo C029/404934/2024 BDAT
2026-01-18
THESE ARE SOME OF THE DATA TYPES IN R
Numeric Data Types
a <- 5 # This is a numeric data type as variable 'a' has been equated to an integer.
## [1] 5
b <- 6 # This gives us a representation of R Language being a case-sensitive language.
## [1] 6
B <- 0
## [1] 0
String or Character Data Types
myChar <- "H" # Example of a character
myChar
## [1] "H"
myString <- "Hello World" # Example of a string
myString
## [1] "Hello World"
myChar <- 'H' # This shows that both double quotes and single quotes may be used.
myChar
1
## [1] "H"
Logical Data Types
myLogical <- T # Logical Representation of True.
myLogical
## [1] TRUE
myLogical2 <- F # Logical Representation of False.
myLogical2
## [1] FALSE
myLogical3 <- TRUE # This shows that the logical representation can be denoted
# with the character or string.
myLogical3
## [1] TRUE
myLogical4 <- FALSE
myLogical4
## [1] FALSE
Complex Data Types
c <- 1 + 3i #This is a complex data type.
c
## [1] 1+3i
Raw Data Type
d <- charToRaw(myChar) # Character to a Raw Data Type using a function.
d
## [1] 48
e <- charToRaw(myString) # String to a Raw Data Type using a function.
e
## [1] 48 65 6c 6c 6f 20 57 6f 72 6c 64
f <- intToBits(a) # Integer to Bits using a function.
f
## [1] 01 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
## [26] 00 00 00 00 00 00 00
Data and Time Data Type
2
date1 <- [Link]("2025-10-31") # This is a date Data Type.
date1
## [1] "2025-10-31"
Data Type Coercion in R
g <- [Link](myLogical) # This changed the logic to a Boolean.
g
## [1] 1
h <- [Link](B) # This changed a Boolean to logic.
h
## [1] FALSE
i <- [Link](a) # This changed an integer to a raw value.
i
## [1] 05
j <- [Link](b) # This changed an integer to a double.
j
## [1] 6
k <- [Link](h) # This changed a String of Logic to a character.
k
## [1] "FALSE"
l <- [Link](myChar) # This introduces a warning.
## Warning: NAs introduced by coercion
## [1] NA
Checking Data Types in R
typeof(a) # Shows the internal storage type of the object.
## [1] "double"
3
class(date1) # Displays the object's class (e.g., "Date", "[Link]").
## [1] "Date"
mode(e) # Returns the object’s general data type (numeric, character, etc.).
## [1] "raw"
[Link](c) # Shows or sets the storage mode (e.g., "integer", "double").
## [1] "complex"
str(myString) # Displays the structure of an object (type and contents summary).
## chr "Hello World"