Data Types in R Programming
Welcome to the first unit of our Business Analysis Techniques course. In this module, we’ll explore the fundamental building blocks of R programming – data types. Understanding data types is crucial for effective data manipulation, analysis, and visualization in business contexts.
Fundamental Data Types in R
R has several basic data types that serve different purposes in data analysis. Let’s explore each one in detail with business-relevant examples.
1. Numeric Data Type
The numeric data type represents real numbers (both integers and decimals). This is the default data type for numbers in R.
Business Application:
Financial metrics, sales figures, percentages, measurements
2. Integer Data Type
Integers are whole numbers without decimal points. In R, we need to explicitly specify when we want integer values.
Business Application:
Employee counts, product quantities, customer IDs
3. Character Data Type
Character data type stores text values (strings). In R, we use quotes to define character values.
Business Application:
Customer names, product categories, department names, addresses
4. Logical Data Type
Logical data type represents Boolean values: TRUE or FALSE. These are essential for conditional operations and filtering data.
Business Application:
Status indicators, eligibility checks, condition evaluations
5. Factor Data Type
Factors are used to represent categorical data. They store both the values and the possible levels (categories).
Business Application:
Customer segments, product types, regions, satisfaction levels
Data Type Comparison and Appropriate Uses
| Data Type | Appropriate Business Use Cases | Key Functions | Memory Considerations |
|---|---|---|---|
| Numeric | Financial calculations, metrics with decimal precision | class(), typeof(), is.numeric() | Uses more memory than integers |
| Integer | Countable items, IDs, quantities | as.integer(), is.integer(), L suffix | More memory efficient for whole numbers |
| Character | Text data, names, descriptions, categories | paste(), nchar(), toupper(), tolower() | Memory usage depends on string length |
| Logical | Boolean conditions, flags, status indicators | is.logical(), & (AND), | (OR), ! (NOT) | Most memory-efficient data type |
| Factor | Categorical data with limited unique values | factor(), levels(), summary() | More efficient than character for repeated categories |
Data Type Conversion
In business analysis, we often need to convert between data types to perform appropriate operations.
Practical Exercises
Exercise 1: Employee Data Analysis
Create variables to store the following employee information with appropriate data types:
- Employee ID (integer)
- Employee Name (character)
- Department (factor with levels: “HR”, “Finance”, “Marketing”, “IT”)
- Salary (numeric)
- Is Manager (logical)
Create data for 5 employees and calculate the average salary by department.
Exercise 2: Sales Performance Evaluation
Create a small dataset for sales representatives with the following information:
- Salesperson Name
- Region (categorical)
- Quarterly Sales (numeric)
- Target Met (logical – TRUE if sales > 100000)
Calculate the percentage of salespeople who met their target and identify the highest performer.
Exercise 3: Product Inventory Management
Create variables to track product inventory:
- Product ID (integer)
- Product Name (character)
- Category (factor)
- Current Stock (integer)
- Reorder Needed (logical – TRUE if stock < 10)
Identify which products need reordering and calculate the total inventory value.
Key Takeaways
- Choosing the right data type improves efficiency and accuracy in business analysis
- Numeric types are ideal for calculations, integers for countable items
- Factors optimize memory usage for categorical data with repeated values
- Logical types are essential for conditional operations and filtering
- Proper data type selection impacts the performance of your R code
In our next session, we’ll explore data structures in R (vectors, matrices, data frames) and how they build upon these fundamental data types for business analysis.
