title: “Data Types in R - Comprehensive Practical Assignment” author: “Luis Figo Azengah C029/404268/2024” date: “r Sys.Date()” output: pdf_document

{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE, warning = FALSE) # Data Types in R - Executable Assignment Author: Luis Figo Azengah
Registration Number: C029/404268/2024
**Date: 31/10/2025 12.09

1. Introduction to Atomic Data Types

In R programming, everything is an object. Atomic data types are the basic building blocks that can be used to create atomic vectors. The main atomic types include:

{r atomic-check} # Check if basic values are atomic vectors is.atomic(3) # TRUE - numeric values are atomic is.atomic(“R CODER”) # TRUE - character strings are atomic is.atomic(TRUE) # TRUE - logical values are atomic

Expected Output: All three checks should return TRUE since these are all atomic data types. 2. Checking Data Types in R

R provides multiple functions to inspect data types, each serving different purposes:

typeof(): Shows internal storage mode class(): Shows object class (can be modified) mode(): Shows computational mode
storage.mode(): Shows storage mode str()`: Shows full structure

{r type-checking} # Different type checking functions on numeric values typeof(1) # “double” - internal storage type class(2) # “numeric” - object class storage.mode(3) # “double” - storage mode
mode(4) # “numeric” - computational mode str(5) # num 5 - full structure

Demonstrate difference between class and typeof

x <- 1 original_class <- class(x) # “numeric” original_type <- typeof(x) # “double”

Change class while internal type remains the same

class(x) <- “My_custom_class” new_class <- class(x) # “My_custom_class” same_type <- typeof(x) # “double” (unchanged)

Expected Output: Shows how class can be modified while internal type remains consistent.

  1. Numeric Data Types

Numeric data in R includes both double (default) and integer types.

3.1 Double/Real Data Type

{r double-type} # Double is the default numeric type in R typeof(2) # “double” - regular number typeof(Inf) # “double” - positive infinity typeof(-Inf) # “double” - negative infinity
typeof(NaN) # “double” - not a number typeof(3.12e3) # “double” - scientific notation typeof(0xbade) # “double” - hexadecimal notation

Verify double type

is.double(2) # TRUE is.double(2.8) # TRUE is.numeric(2) # TRUE - double is numeric

3.2 Integer Data Type {r integer-type} # Integer requires ‘L’ suffix y <- 2L typeof(y) # “integer”

Integer checking

is.integer(3) # FALSE - without L is double is.integer(3L) # TRUE - with L is integer is.numeric(3L) # TRUE - integer is also numeric

General numeric check

mode(55) # “numeric” is.numeric(3) # TRUE

4. Logical Data Type

Logical values represent Boolean logic with TRUE, FALSE, and NA. {r logical-type} # Basic logical values t <- TRUE f <- FALSE
n <- NA

typeof(t) # “logical” typeof(f) # “logical” typeof(n) # “logical”

Logical checking

is.logical(T) # TRUE is.logical(TRUE) # TRUE

Demonstration of T/F vs TRUE/FALSE difference

initial_F <- F # FALSE a <- T # Assign T to variable F <- a # Reassign F (not recommended!) new_F <- F # Now TRUE instead of FALSE

Note: This demonstrates why using TRUE/FALSE is safer than T/F

Expected Output:** Shows logical type consistency and the danger of reassigning T/F.

  1. Complex Data Type

Complex numbers include imaginary components.

{r complex-type} # Complex number with imaginary part z <- 1 + 3i typeof(z) # “complex” is.complex(z) # TRUE

Another complex example

complex_num <- 2 + 4i Re(complex_num) # Real part: 2 Im(complex_num) # Imaginary part: 4

  1. Character Data Type

Strings of text enclosed in single or double quotes.

{r character-type} # Character strings character <- “a” typeof(character) # “character” is.character(character) # TRUE

Quote equivalence

typeof(‘R CODER’) # “character” - single quotes typeof(“R CODER”) # “character” - double quotes

String operations

nchar(“A string”) # 8 - counts characters including spaces nchar(“Hello World!”) # 12 - punctuation also counted

  1. Raw Data Type

Raw type stores bytes and is used for low-level data.

{r raw-type} # Convert character to raw bytes a <- charToRaw(“R CODER”) a # Raw bytes: 52 20 43 4f 44 45 52 typeof(a) # “raw”

Convert integer to raw bits

b <- intToBits(3L) typeof(b) # “raw” is.raw(b) # TRUE

Display first few bits

head(b) # Show first elements

  1. Date and Time Data Type

Special types for handling dates and times.

{r datetime-type} # Date handling dates <- c(“14-08-1947”, “06-09-1965”) d <- as.Date(dates, “%d-%m-%Y”) class(d) # “Date” formatted_dates <- format(d, “%d-%m-%Y”) formatted_dates

Time handling

times <- c(“23:03:20”, “22:29:56”) t <- strptime(times, “%H:%M:%S”) class(t) # “POSIXlt” “POSIXt”

Current date and time

current_date <- Sys.Date() current_time <- Sys.time() current_date current_time

  1. Data Type Coercion

Converting between types using as. () functions.

{r coercion} # Double to integer a <- 3 original_type <- typeof(a) # “double” a <- as.integer(a) new_type <- typeof(a) # “integer”

Logical to numeric

b <- TRUE b_numeric <- as.numeric(b) # 1 (TRUE becomes 1) b_numeric

c <- FALSE
c_numeric <- as.numeric(c) # 0 (FALSE becomes 0) c_numeric

Logical to character

d <- TRUE d_character <- as.character(d) # “TRUE” d_character

Numeric to character

num_val <- 42.7 char_val <- as.character(num_val) # “42.7” char_val

Character to numeric (with warning)

text <- “R CODER” text_numeric <- as.double(text) # NA with warning text_numeric

Expected Output: Demonstrates successful coercions and shows what happens with incompatible types.

  1. Comprehensive Type Checking Table

{r type-table} # Create examples of all major types types_list <- list( double = 3.14, integer = 3L, logical = TRUE, complex = 1+2i, character = “hello”, raw = charToRaw(“A”) )

Check each type with different functions

type_info <- sapply(types_list, function(x) { c( typeof = typeof(x), class = class(x), mode = mode(x), storage.mode = storage.mode(x) ) })

print(“Type Checking Results:”) type_info ```

Conclusion

This comprehensive practical assignment demonstrates:

1.R’s type system is built around atomic data types that form the foundation of all data structures 2. Multiple checking functions- serve different purposes, with typeof() being most reliable for internal type 3. Type coercion- is powerful but requires understanding of compatibility between types 4. Everything in R is an object-, and understanding types is essential for effective programming 5. Proper type handling- prevents errors and ensures accurate data analysis