Meritshot Tutorials
- Home
- »
- R-Data Types
SQL Tutorial
-
R-OverviewR-Overview
-
R Basic SyntaxR Basic Syntax
-
R Data TypesR Data Types
-
R-Data StructuresR-Data Structures
-
R-VariablesR-Variables
-
R-OperatorsR-Operators
-
R-StringsR-Strings
-
R-FunctionR-Function
-
R-ParametersR-Parameters
-
Arguments in R programmingArguments in R programming
-
R String MethodsR String Methods
-
R-Regular ExpressionsR-Regular Expressions
-
Loops in R-programmingLoops in R-programming
-
R-CSV FILESR-CSV FILES
-
Statistics in-RStatistics in-R
-
Probability in RProbability in R
-
Confidence Interval in RConfidence Interval in R
-
Hypothesis Testing in RHypothesis Testing in R
-
Correlation and Covariance in RCorrelation and Covariance in R
-
Probability Plots and Diagnostics in RProbability Plots and Diagnostics in R
-
Error Matrices in RError Matrices in R
-
Curves in R-Programming LanguageCurves in R-Programming Language
R-Data Types
When it comes to working with data in R, understanding data types is fundamental. Just like in real life, where you have different types of objects like numbers, words, and lists, R also categorizes data into various types. These data types help R understand how to handle and manipulate your data
There are numerous data types in R programming language, including integer, string, and more. Based on the variable’s data type, the operating system allows memory and
determines what can be kept in the reserved memory. This article will discuss all the data types in R Programming Language so keep reading the blog.
What are R Data types?
In R, data types represent the kind of data that can be stored and manipulated. Common data types include numeric (for numbers), character (for text), logical (for true/false values), and factors (for categorical data). R also supports more specialized types like lists and data frames for structured data storage. These data types help organize and work with diverse data in R programming.
Different R- Data Types
There are the following data types which are used in R programming:
- Numeric
- Integer
- Logical
- Complex
- Character
- Raw
Numeric Data Type
For numerical values, there is the numeric data type. It is a standard data type in r programming for numbers.
The following are some examples of numerical values: 2, 25.4, -45.253, 48.02, etc.
Decimal values are called Numerics in R. It is the default computational data type. If we assign a decimal value to a variable x as follows, x will be of numeric type.
Examples of Numeric Data Type:
(i)
x <- 10.5 # Assigning A Decimal Value print(x) # Print the Value Of x
[1] 10.5 #Output
print(class(x)) # Print the class name of x
[1] “numeric” #Output (ii)
num <- -250 #Assigning a negative numeric value print(class(num)) #Print the class of num, Output = “numeric” print(typeof(num)) #Print the type of num, Output = “double”
(iii)
x <- 42.5 # Numeric (floating-point number)
y <- 100 # Numeric (integer stored as numeric) print(x) #Print the value of x , Output = 42.5 print(y) #Print the value of y, Output = 100
Even though num is an integer-like value, R assigns it as a “numeric” class and a “double” type because numeric values in R are stored as floating-point numbers by default.
Daily Life Example: Think of a numeric value as the amount on a restaurant bill or the temperature recorded in degrees. Numeric types are used whenever you’re working with numbers that can have decimal points.
Scenario: Recording a Financial Transaction
Imagine you’re managing the finances for a small business. You need to keep track of your daily expenses, and one day, you record a negative balance because of a refund to a customer.
expense <- -250 # Recording a negative expense due to a refund print(expense) # Print the recorded expense
Output:
[1] -250
print(class(expense)) # Print the class of expense
Output:
[1] “numeric”
print(typeof(expense)) # Print the type of expense
Output:
[1] “double”
Explanation: In this scenario, you’re tracking expenses, and a refund (negative amount) is represented as a numeric value. Although it’s an integer-like number, R stores it as a
“numeric” class and “double” type, allowing it to handle decimal or fractional values if necessary (like taxes or interest calculations). This flexibility is useful for financial calculations.
Integer Data Type
The Integer data type represents integer values. We must specify a value as an integer to store it as one. The integer data type is frequently employed for discrete- only values like unique IDs. The as.integer() function allows us to store and convert a value into an integer type.
In R, integers are a specific type of numeric data used to represent whole numbers. While the numeric data type is the default for all numbers (whether integer or decimal), integers can be explicitly defined by appending an “L” to the value. For example, 5L represents an integer, whereas 5 is stored as numeric (double).
The following are some examples of integer values: 5L, 100L, -45L, 0L, etc.
Examples of Integer Data Types:
(i)
x <- 10L # Assigning an integer value using L print(x) # Print the value of x
Output [1] 10
print(class(x)) # Print the class of x
Output
[1] “integer”
(ii)
a <- 5L # Assigning an integer value
b <- 3L # Assigning another integer value
result <- a + b # Integer arithmetic (addition)
print(result) # Print the result
Output:
[1] 8
print(class(result)) # Print the class of result
Output:
[1] “integer”
- We can also declare an integer by appending an L y = 3L
is.integer(y) # is y an integer?
Output:
TRUE
Daily Life Example: Counting Items in a Store
Integers are ideal for representing things that cannot be divided into parts. For instance, the number of items in a shopping cart or the number of employees in a company must be whole numbers (integers).
Scenario: Counting Items in a Warehouse
Imagine you are managing a warehouse and need to track the stock count of different items. The number of items must be a whole number because you cannot have half of a product in stock.
stock_count <- 150L # Recording stock count as an integer print(stock_count) # Print the stock count
Output: [1] 150
print(class(stock_count)) # Print the class of stock_count
Output:
[1] “integer”
print(typeof(stock_count)) # Print the type of stock_count
Output:
- “integer”
Explanation: In this scenario, you need to ensure that the stock count is always a whole number. Since fractional products are not possible, using an integer data type ensures that the count is accurate and appropriate for this kind of data.
Key Differences b/w Numeric and Integer Data Types
Numeric is the default type for numbers, and it can store both integers and decimal values. Integer, on the other hand, is specifically for whole numbers and requires explicit definition using L.
Multiline Comments
Unlike other programming languages, such as Java, there are no syntax in R for multiline comments. However, we can just insert a # for each line to create multiline comments
Logical Data Types
Logical data types in R programming accept either a true or false value. It is common to compare two variables to arrive at a logical value.
In R, the logical data type is used to represent boolean values, which can either be TRUE or FALSE. Logical data types are fundamental for conditions, comparisons, and controlling the flow of programs.
Characteristics of Logical Data Type in R:
- Values:
- Only two possible values: TRUE and
- Shorthand T and F can also be used, but it’s recommended to use TRUE and FALSE to avoid potential issues if T or F are
- Numeric Representation:
- In R, TRUE is internally stored as 1, and FALSE is stored as This allows logical values to be used in mathematical operations.
Examples of Logical Values in R:
Assigning Logical Values
is_valid <- TRUE # Assigning TRUE to is_valid print(is_valid)
# Output: TRUE
print(class(is_valid))
# Output: “logical”
Explanation:
- is_valid is a logical variable that holds the value TRUE.
- print() outputs the value stored in
- class() tells you the data type of is_valid, which is “logical”.
Using Logical in Comparisons x <- 10
y <- 20
result <- x < y # Comparing if x is less than y
print(result) # Output: TRUE
print(class(result)) # Output: “logical”
Explanation:
- The comparison x < y evaluates to TRUE because 10 is less than
- result stores this logical value (TRUE), and class(result) confirms that it’s of the logical type.
Example 2: Basic Arithmetic Operation
Logical with Numeric Operations
logical_sum <- TRUE + 5 # TRUE is treated as 1
logical_diff <- FALSE + 10 # FALSE is treated as 0 print(logical_sum) # Output: 6
print(logical_diff) # Output: 10
Explanation:
- In R, TRUE is treated as 1 and FALSE as 0 when used in arithmetic
- Adding TRUE to 5 gives 6 and adding FALSE to 10 gives
Daily life Example: Checking if a Student Passed or Failed
Imagine you’re managing student grades and need to determine if each student has passed or failed based on their score.
In R, you can use logical data types to perform this check. Logical values in R are TRUE or FALSE.
Scenario
You have a list of students with their scores, and the passing mark is 50. You want to create a logical vector indicating whether each student has passed.
Data
students_scores <- c(45, 67, 82, 49, 55)
students_scores <- c(45, 67, 82, 49, 55)
You can create a logical vector to check if each student has passed:
passing_mark <- 50
passed <- students_scores >= passing_mark
Output
- FALSE TRUE TRUE FALSE TRUE
Here, the logical vector passed tells you whether each corresponding score in students_scores meets or exceeds the passing mark. In this case:
- FALSE indicates the student did not
- TRUE indicates the student
You can then use this logical vector to perform further actions, like calculating the percentage of students who passed, or filtering out the students who failed.
Complex Data Types
Complex data types in r programming are used to hold numbers that have fictitious ` components. Complex values include things like 2+3i, 1i, 5-4i, etc.
A complex value in R is defined as the pure imaginary value i.
For Example: Z=1+2i, t=7+3i
Complex data types in R are more advanced structures that allow for the storage and manipulation of more complex data. They are essential for handling diverse datasets and performing sophisticated operations. Here’s an overview of the key complex data types:
1. Vectors
Scenario: Storing Daily Temperatures
Imagine you’re tracking the daily high temperatures over a week. You can use a vector to hold these temperatures.
Example:
# Daily high temperatures for a week
daily_temperatures <- c(72, 75, 78, 79, 74, 70, 68)
Explanation:
- daily_temperatures is a simple list of numbers, representing temperatures for each
2. Matrices
Scenario: Weekly Sales Data
Consider a store that tracks sales data for each day of the week and each product category. A matrix can represent this data.
Example:
# Sales data: Rows represent days, columns represent product categories sales_data
<- matrix(c(200, 150, 100, 220, 180, 110, 210, 190, 140, 105, 230, 175, 115, 215),
nrow = 2, byrow = TRUE)
Explanation:
- sales_data is organized into rows (days) and columns (categories). Each entry represents sales
3. Arrays
Scenario: Inventory Tracking
Imagine a warehouse that stores multiple types of items across different locations and times. An array can keep track of this information.
Example:
# Inventory levels: Dimensions are (locations, items, times)
inventory_levels <- array(c(50, 60, 55, 45, 65, 70, 40, 50, 45, 35, 55, 60),
dim = c(2, 3, 2))
Explanation:
- inventory_levels tracks inventory with dimensions representing locations, items, and
4. Lists
Scenario: Personal Information
Consider you want to store different pieces of personal information (name, age, hobbies) about individuals. A list can hold various types of data.
Example:
# Personal information
person_info <- list(name = “Alice”, age = 30, hobbies = c(“Reading”, “Hiking”)) Explanation:
- person_info can store different types of data (character, numeric, vector) in one
5. Data Frames
Scenario: Contact List
Imagine you maintain a contact list with names, phone numbers, and email
addresses. A data frame is ideal for organizing this information in a tabular format.
Example:
# Contact list contacts <- data.frame( Name = c(“Roshan”, “Chalsee”, “Meritshot”), Phone = c(“555-1234”, “555-5678”, “555-8765”), Email = c(“roshan@example.com“, “chalsee@example.com“, “meritshot@example.com“) )
Explanation:
- contacts is a table where each row represents a person and each column represents a type of information.
6. Factors
1. Factors
Consider you conduct a survey where respondents rate their satisfaction on a scale of “Low”, “Medium”, and “High”. Factors help to handle categorical responses.
Example:
# Survey satisfaction ratings
satisfaction_ratings <- factor(c(“Medium”, “High”, “Low”, “Medium”, “High”))
Explanation:
satisfaction_ratings categorizes responses into predefined levels: Low, Medium,
num_vector <- c(1.1, 2.2, 3.3)
print(num_vector) # Output: 1.1 2.2 3.3
print(class(num_vector)) # Output: “numeric”
Creating Character Vectors
char_vector <- c(“apple”, “banana”, “cherry”)
print(char_vector) # Output: “apple” “banana” “cherry” print(class(char_vector)) # Output: “character”
Explanation:
- num_vector stores numeric values and char_vector stores character
- class() shows the type of each
These examples provide a general understanding of complex data types and how they can be used in various scenarios.
Character Data Types
Character data types in r programming are used to hold strings or character values. They are essential for handling strings, which can include names, descriptions, or any other textual information.
A character object is used to represent string values in R. We convert objects into character values with the as.character() function.
# Creating a character object char <- “Roshan Sharma”
print(class(char)) print(typeof(char))
Output
- “character” [1]”character”
-> Converting Other Data Types to Character
Scenario: Converting a Numeric ID to a Character String
When you need to convert a numeric ID to text.
# Converting numeric ID to character .
id <- 12345
char_id <- as.character(id)
print(char_id) #
Output: “12345”
print(class(char_id)) #
Output: “character”
Explanation:
- character() converts the numeric id to a character string.
# Two character values can be concatenated with the paste function.
# Define the first and last names fname <- “Roshan”
lname <- “Sharma”
# Combine the names using paste full_name <- paste(fname, lname)
# Print the full name
print(full_name) # Output: “Roshan Sharma””
Raw- Data Types
In R, the raw data type is used to represent raw bytes of data, which is useful for handling binary data. Unlike other data types in R, which represent data in a more human-readable form, the raw type is specifically designed to work with raw binary data.
Understanding the raw Data Type
Characteristics of raw Data Type:
- Binary Representation:
- The raw type stores data as raw bytes (binary data). Each byte can be any value from 0 to
2. Hexadecimal Format:
- When printed, raw data is often displayed in hexadecimal Each byte is represented as a pair of hexadecimal digits.
3. Use Cases:
- Handling binary files, network communications, or other low-level data manipulations where direct control over raw bytes is
Examples of Using raw Data Type
# Creating raw data
raw_data <- as.raw(c(0x41, 0x42, 0x43)) # Represents “ABC” in hexadecimal print(raw_data) # Output: [1] 41 42 43
Explanation:
- raw(c(0x41, 0x42, 0x43)) creates a raw vector with three bytes, corresponding to the hexadecimal values for the ASCII characters “A”, “B”, and “C”.
# Reading a binary file as raw data
file_path <- “example.bin” # Replace with your file path
raw_data <- readBin(file_path, what = “raw”, n = file.info(file_path)$size) print(raw_data)
Explanation:
readBin() reads binary data from a file into a raw vector. The what = “raw” argument specifies that the data should be read as raw bytes.
The raw data type is essential for working with low-level binary data in R. It allows you to handle and manipulate data at the byte level, which is useful for tasks such as binary file processing and network communication.