0% found this document useful (0 votes)
12 views12 pages

Data Types in R

Data Types in R

Uploaded by

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

Data Types in R

Data Types in R

Uploaded by

Chetan Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

R Data Types

Data Types in R are:


1.numeric – (3, 6.7, 121)
2.Integer – (2L, 42L; where ‘L’ declares this as an
integer)
3.logical – (‘True’)
4.complex – (7 + 5i; where ‘i’ is imaginary number)
5.character – (“a”, “B”, “c is third”, “69”)
6.raw – (as.raw(55); raw creates a raw vector of the
specified length)

What is the difference between class


and typeof function in R?
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"

Character “a”, “b”, “c”, …, “@”, “#”, "character_value <-


“$”, …., “1”, “2”, …etc "Hello Geeks"

raw as.raw() "single_raw <-


as.raw(255)"
Numeric Data type in R
Decimal values are called numeric in R.
It is the default R data type for numbers in R.
Any decimal value assigned to a variable x, 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.
# A simple R program to illustrate Numeric data type
# Assign a decimal value to x
x = 5.6
Output
# print the class name of variable
[1] "numeric"
print(class(x))
[1] "double"
# print the type of variable
print(typeof(x))
Integer Data type in R
R supports integer data types which are the set of all integers.
You can create as well as convert a value into an integer type
using the as.integer() function.
You can also use the capital ‘L’ notation as a suffix to denote that
a particular value is of the integer R data type
# A simple R program to illustrate integer data type
# Create an integer value
x = as.integer(5)
# print the class name of x
print(class(x))
# print the type of x
print(typeof(x))

# Declare an integer by appending an L suffix.


y = 5L Output
# print the class name of y [1] "integer“
print(class(y)) [1] "integer"
# print the type of y [1] "integer“
print(typeof(y)) [1] "integer"
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
# A simple R program to illustrate logical data type
# Sample values
x=4
y=3
# Comparing two values Output
z=x>y [1] TRUE
# print the logical value [1] "logical"
print(z) [1] "logical"
# print the class name of z
print(class(z))
# print the type of z
print(typeof(z))
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.

# A simple R program to illustrate complex data type

# Assign a complex value to x


x = 4 + 3i

# print the class name of x Output


print(class(x)) [1] "complex"
[1] "complex"
# print the type of x
print(typeof(x))
Character Data type in RR
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.
# A simple R program to illustrate character data type

# Assign a character value to char


char = "Geeksforgeeks"
Output
[1] "character"
# print the class name of char
[1] "character"
print(class(char))

# print the type of char


print(typeof(char))
# A simple R program to illustrate integer data type
# Create an integer value
x = as.integer(5)
# print the class name of x
print(class(x))
# print the type of x
print(typeof(x))

# Declare an integer by appending an L suffix.


y = 5L Output
# print the class name of y [1] "integer“
print(class(y)) [1] "integer"
# print the type of y [1] "integer“
print(typeof(y)) [1] "integer"
Raw data type in R
Use the raw data type to save and work with data at the byte
level in R.
By displaying a series of unprocessed bytes, it enables low-level
operations on binary data.
Each byte in a raw vector is a value between 0 and 255.
Here are some speculative data on R’s raw data types:
# Create a raw vector
x <- as.raw(c(0x1, 0x2, 0x3, 0x4, 0x5))
print(x)
Output[1] 01 02 03 04
05

Five elements make up this raw vector x, each of


which represents a raw byte value.
Coerce or Convert the Data Type of an Object to Another
The process of altering the data type of an object to another type
is referred to as coercion or data type conversion.
This is a common operation in many programming languages that
is used to alter data and perform various computations.

When coercion is required, the language normally performs it


automatically, whereas conversion is performed directly by the
programmer.

Coercion can manifest itself in a variety of ways, depending on


the R programming language and the context in which it is
employed.

In some circumstances, the coercion is implicit, which means that


the language will change one type to another without the
programmer having to expressly request it.
# A simple R program convert data type of an object to another
# Logical
print(as.numeric(TRUE))
Output
# Integer [1] 1
print(as.complex(3L)) [1] 3+0i
[1] TRUE
# Numeric [1] "1+2i"
print(as.logical(10.5)) [1] NA
Warning message:
# Complex Inprint(as.numeric("12-
print(as.character(1+2i)) 04-2020")) : NAs
introduced by coercion
# Can't possible
print(as.numeric("12-04-2020"))

You might also like