When working in R, understanding data types is essential. Data types determine what kind of values you can store in a variable. For instance, numbers, text, or logical values (like TRUE and FALSE). Let’s break it down step by step.
1. Numeric (Numbers)
- This is the most common data type in R.
- It includes both whole numbers and decimal numbers.
Example:
x <- 10 # A whole number
y <- 15.5 # A decimal number
- You can perform calculations with numeric data:
z <- x + y # Adds 10 and 15.5
print(z) # Output: 25.5
2. Character (Text or Strings)
- Used to store text.
- Characters must be wrapped in quotation marks (single or double).
Example:
name <- "Alice" # Double quotes
greeting <- 'Hi!' # Single quotes
Combining Characters:
full_sentence <- paste("Hello,", name)
print(full_sentence) # Output: Hello, Alice
3. Logical (TRUE or FALSE)
- This is used to store TRUE or FALSE values.
- Often used for conditions or decision-making.
Example:
is_raining <- TRUE
has_umbrella <- FALSE
Logical Operations:
is_safe <- is_raining & has_umbrella
print(is_safe) # Output: FALSE (because you don’t have an umbrella)
4. Integer (Whole Numbers)
- Like numeric, but explicitly stores only whole numbers.
- Use the
L
suffix to create an integer.
Example:
age <- 25L
print(class(age)) # Output: "integer"
- Integers are less common because R treats most numbers as numeric by default.
5. Factor (Categorical Data)
- Used for data that belongs to categories or groups.
- Great for organizing things like gender, colors, or regions.
Example:
gender <- factor(c("Male", "Female", "Female", "Male"))
print(gender)
Levels:
Factors include unique levels (categories):
print(levels(gender)) # Output: "Female" "Male"
6. Complex Numbers
- Stores numbers with both a real and imaginary part.
Example:
complex_num <- 3 + 2i
print(complex_num) # Output: 3 + 2i
print(class(complex_num)) # Output: "complex"
7. Raw Data
- Used for storing raw bytes (rarely used in basic data analysis).
Example:
raw_data <- charToRaw("Hello")
print(raw_data) # Output: 48 65 6c 6c 6f
Additional Notes
Checking the Data Type
You can use class()
to check the type of any variable:
x <- 42
print(class(x)) # Output: "numeric"
Converting Data Types
Sometimes, you may need to convert a variable from one type to another. Here are some common conversion functions:
as.numeric()
– Converts to numeric.as.character()
– Converts to character.as.logical()
– Converts to logical.as.integer()
– Converts to integer.
Example:
num <- 10
text <- as.character(num)
print(class(text)) # Output: "character"
Quick Summary of R Data Types
Data Type | Description | Example |
---|---|---|
Numeric | Numbers (whole or decimal) | 10 , 5.7 |
Character | Text or strings | "Hello" , 'World' |
Logical | TRUE or FALSE values | TRUE , FALSE |
Integer | Whole numbers (explicit) | 25L , 100L |
Factor | Categorical data | factor(c("Red", "Blue")) |
Complex | Real and imaginary numbers | 3 + 4i |
Raw | Raw bytes | charToRaw("Data") |
By understanding these basic data types, you’ll be better equipped to handle any data you encounter in R. Practice using them, and soon, they’ll feel second nature!