R supports various data types that help you store and manipulate different kinds of information. Understanding data types is crucial for effective programming in R.
5, 3.14
5L
(L
denotes integer)"Hello"
TRUE
or FALSE
3+2i
You can check the data type of a value or variable using the class()
function.
x <- 10 class(x) # Returns "numeric" y <- "R Programming" class(y) # Returns "character" z <- TRUE class(z) # Returns "logical"
num <- 25 # Numeric int_num <- 25L # Integer text <- "Data" # Character flag <- FALSE # Logical comp <- 4+5i # Complex number print(class(num)) print(class(int_num)) print(class(text)) print(class(flag)) print(class(comp))
Remember, R automatically converts data types when needed (called coercion). For example, mixing numbers and text in a vector converts everything to text.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!