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

Understanding R Data Types Explained

The document outlines the various data types in R, including Numeric, Integer, Logical, Complex, Character, and Raw, each with specific characteristics and examples. It emphasizes the importance of choosing the correct data type for memory optimization and computation efficiency. Additionally, it explains how R allows for dynamic type changes without explicit declarations.
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 views7 pages

Understanding R Data Types Explained

The document outlines the various data types in R, including Numeric, Integer, Logical, Complex, Character, and Raw, each with specific characteristics and examples. It emphasizes the importance of choosing the correct data type for memory optimization and computation efficiency. Additionally, it explains how R allows for dynamic type changes without explicit declarations.
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

Data Types

• Data types in R define the kind of values that variables can hold. Choosing the
right data type helps optimize memory usage and computation. Unlike some
languages, R does not require explicit data type declarations while variables can
change their type dynamically during execution.
• R Programming language has the following basic R-data types and the following
table shows the data type and the values that each data type can take.
Basic Data Types Values Examples

Numeric Set of all real numbers "numeric_value <- 3.14"

Integer Set of all integers, Z "integer_value <- 42L"

Logical TRUE and FALSE "logical_value <- TRUE"

Complex Set of complex numbers "complex_value <- 1 + 2i"

"a", "b", "c", ..., "@", "#", "$", ...., "1",


Character "character_value <- "Hello Geeks"
"2", ...etc

raw [Link]() "single_raw <- [Link](255)"


1. Numeric Data type in R
Decimal values are called numeric in R. It is the default R data type for numbers in R. If we assign a decimal
value to a variable x as follows, x will be of numeric type.
Real numbers with a decimal point are represented using this data type in R. It uses a format for double-
precision floating-point numbers to represent numerical values.
x = 5.6
print(class(x))
print(typeof(x))
Output[
1] "numeric“
[1] "double"
Even if an integer is assigned to a variable y, it is still saved as a numeric value.
y=5
print(class(y))
print(typeof(y))
Output
[1] "numeric"
[1] "double"
2. Integer Data type in R
R supports integer data types which are the set of all integers. we can create as well as convert a value into
an integer type using the [Link]() function.
we can also use the capital 'L' notation as a suffix to denote that a particular value is of the integer R data
type.
x = [Link](5)
print(class(x))
print(typeof(x))
y = 5L
print(class(y))
print(typeof(y))

Output
[1] "integer"
[1] "integer"
[1] "integer"
[1] "integer"
3. Logical Data type in R
R has logical data types that take either a value of true or false.
A logical value is often created via a comparison between variables.
Boolean values, which have two possible values, are represented by this R data type: FALSE or TRUE
x=4
y=3
z=x>y
print(z)
print(class(z))
print(typeof(z))

Output
[1] TRUE
[1] "logical"
[1] "logical"
4. Complex Data type in R
R supports complex data types that are set of all the complex numbers.
The complex data type is to store numbers with an imaginary component.
x = 4 + 3i
print(class(x))
print(typeof(x))
Output[1] "complex"
[1] "complex"
5. Character Data type in R
R supports character data types where we have all the alphabets and special characters. It stores character values or strings.
Strings in R can contain alphabets, numbers, and symbols.
The easiest way to denote that a value is of character type in R data type is to wrap the value inside single or double inverted
commas.
char = “R Programming"
print(class(char))
print(typeof(char))

Output[1] "character" [1] "character"


6. Raw data type in R
To save and work with data at the byte level in R, use the raw data type. By displaying a series of unprocessed
bytes, it enables low-level operations on binary data.
Here are some speculative data on R's raw data types:
x <- [Link](c(0x1, 0x2, 0x3, 0x4, 0x5))
print(x)
Output[1] 01 02 03 04 05

You might also like