R Tutorial



R DATA TYPES


Data Types in R

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.

Common Data Types in R

  • Numeric: Represents numbers (both integers and decimals). Example: 5, 3.14
  • Integer: Represents integer numbers explicitly. Example: 5L (L denotes integer)
  • Character: Represents text or strings. Example: "Hello"
  • Logical: Represents Boolean values: TRUE or FALSE
  • Complex: Represents complex numbers with real and imaginary parts. Example: 3+2i

Checking Data Types

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"
  

Example: Using Different Data Types

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))
  

Quick Tip:

Remember, R automatically converts data types when needed (called coercion). For example, mixing numbers and text in a vector converts everything to text.


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review