 R Class

Class

Syntax
//Basic Syntax :
x <- 123
y <- 234
z <- sum(x,y)
print(z)
 357

The above code is explained below:

• x and y are objects in R
• the objects x and y hold the values 123 and 234 respectively. Based on the kind of values an object holds, their datatype is decided and assigned. Datatype in R is known as class of an object.
• sum() & print() are functions
• # is used to place comments
• <- is the assignment operator

Class :

Every object is defined with any one of the following Class

Logical: logical values have only 2 outputs- TRUE or FALSE, example – TRUE, FALSE

CODE/PROGRAM/EXAMPLE
a <- TRUE
class(a)
 "logical"

Numeric : numeric values can be decimal numbers as well as integers, example – 12.3, 5, 999

CODE/PROGRAM/EXAMPLE
b <- 13.5
class(b)
 "numeric"

c <- 23
class(c)
 "numeric"

Integer : placing ‘L’ at the end would store the value as integer, example – 2L, 34L, 0L

CODE/PROGRAM/EXAMPLE
d <- 2L
class(d)
 "integer"

Classes are also called as Data Types of an object

Complex: a complex value in R is defined via the pure imaginary value ‘i’, example - 4 + 3i

CODE/PROGRAM/EXAMPLE
x <- 4+3i
class(x)
 "complex"

Character: text is stored as character type in R, example - “Hello World”, “TRUE”, ‘53.2’, “a”

CODE/PROGRAM/EXAMPLE
x <- "Hello World"
class(x)
 "character"

Raw: the raw type is intended to hold raw bytes, example - “Bookofnetwork” is stored as 51 6f 67 6e 70 80 75 55 44 39 87 97 93

CODE/PROGRAM/EXAMPLE
x <- "Bookofnetwork"
x <- charToRaw(x)
class(x)
 "raw"
print(x)
 51 6f 67 6e 70 80 75 55 44 39 87 97 933