0% found this document useful (0 votes)
6 views5 pages

R Data Types Explained: A Comprehensive Guide

Uploaded by

kamiru23.alex
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

R Data Types Explained: A Comprehensive Guide

Uploaded by

kamiru23.alex
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ASSIGNMENT R

ALEX KAMIRU MBUGUA

2024-10-28

R Script: Data Types in R

Check if an object is atomic


[Link](3) # TRUE: 3 is an atomic object [Link](“R CODER”) # TRUE: “R CODER” is also atomic

Checking data types using various functions

R internal type or storage mode of any object


typeof(1) # “double”: 1 is a double class(2) # “numeric”: class of 2 is numeric [Link](3) # “double”:
storage mode of 3 is double mode(4) # “numeric”: mode of 4 is numeric str(5) # num 5: structure of 5 is
numeric

Example of changing the class of an object


x <- 1 # Assign 1 to x class(x) # “numeric”: original class is numeric class(x) <- “My_class” # Change
class of x to My_class class(x) # “My_class”: class is now My_class typeof(x) # “double”: type remains
double

1
Summary of outputs from various functions

Function outputs summarized in the following table

typeof | [Link] | mode

logical | logical | logical

integer | numeric | integer

double | numeric | double

complex | complex | complex

character | character | character

raw | raw | raw

Numeric data types

Check if an object is numeric


mode(55) # “numeric”: mode of 55 is numeric [Link](3) # TRUE: 3 is numeric

Double or real data type

Demonstrate that all numbers are doubles by default


typeof(2) # “double” typeof(Inf) # “double”: Infinity typeof(-Inf) # “double”: Negative Infinity
typeof(NaN) # “double”: Not a number typeof(3.12e3) # “double”: Scientific notation typeof(0xbade) #
“double”: Hexadecimal

Check if an object is a double


[Link](2) # TRUE: 2 is a double [Link](2.8) # TRUE: 2.8 is also a double

2
Integer data type

Create an integer by appending L


y <- 2L # y is now an integer typeof(y) # “integer”: type is integer [Link](3) # FALSE: 3 is not an
integer [Link](3L) # TRUE: 3L is an integer

Logical data type

Define logical values


t <- TRUE f <- FALSE n <- NA

Check types of logical values


typeof(t) # “logical”: type is logical typeof(f) # “logical”: type is logical typeof(n) # “logical”: type is
logical

Verify logical type


[Link](T) # TRUE: T (TRUE) is logical [Link](TRUE) # TRUE: TRUE is logical

Caution on using T and F


F # This will show the value of F (FALSE) a <- T # Assign TRUE to a F <- a # Overwriting F with the
value of a F # Now F is TRUE

Complex data type

Define a complex number


complex_num <- 1 + 3i typeof(complex_num) # “complex”: type is complex [Link](complex_num)
# TRUE: complex_num is a complex number

String or character data type

Define a character string


character <- “a” typeof(character) # “character”: type is character [Link](character) # TRUE: char-
acter is indeed a character

3
Check with single and double quotes
typeof(‘R CODER’) # “character”: single quotes also work typeof(“R CODER”) # “character”: double
quotes work too

Count characters in a string


nchar(“A string”) # 8: counts letters including spaces

Raw data type in R

Define raw data using charToRaw


raw_data <- charToRaw(“R CODER”) raw_data # Displays raw bytes typeof(raw_data) # “raw”: type
is raw

Convert integer to raw


int_data <- intToBits(3L) typeof(int_data) # “raw”: type is raw [Link](int_data) # TRUE: int_data is
raw

Date and Time data type in R

Create a Date object


date_example <- [Link](“2023-10-28”) # Specify a date format(date_example) # Display the date in
standard format

Coercion of data types in R

Coerce various types using as. functions


a <- 3 # a is a double a <- [Link](a) # Coerce to integer typeof(a) # “integer”: now a is an integer

Coerce logical to numeric


b <- TRUE b <- [Link](b) # TRUE becomes 1 c <- FALSE c <- [Link](c) # FALSE becomes 0
d <- TRUE d <- [Link](d) # TRUE becomes “TRUE”

4
Attempt to coerce incompatible types
[Link](“R CODER”) # Results in NA # Warning: NAs introduced by coercion

End of R Script

You might also like