3.1 Instrinstic attributes: mode and length

R operates on objects(entities to work with).

Mode

  • Vectors of numeric. complex or logical values are known as Atomic structures since their components are all of the same type/mode.
    • Vectors must have their values of same mode.
    • A vector can be empty and still have a mode at same time.
  • R also operates on lists. which will belongs to mode lists. These are ordered sequence of objects which individually can be of any mode. Lists are known as recursive rather than atomic structures since their components can themselves be lists in their own space.

Other recursive structure are those of mode functions and expressions.

When we talked about mode of an object, we are talking about the basic type of its fundamental constituents.This is the special case of a property of object.

Length

Length is the number of elements that is present in an object, or we can say that number of elements that are stored against an object.

  • The length and mode of an object can be found by using the following functions:
  mode(object_name) # return the mode of an object
  length(object_name) # returns length of an object
  • We can also find other attributes of an object by using following function:
  attributes(object_name) # it will return attributes of an object if any

Changing mode of an object

We can change the mode of any object if needed to do so, Like

# R caters for change of mode
x <- 0:9 
x #[1] 0 1 2 3 4 5 6 7 8 9
##  [1] 0 1 2 3 4 5 6 7 8 9
# as.character()/ as.type() will change the mode
digits <- as.character(x)
digits # [1] "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
##  [1] "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
# further coercion/change of mode
d <- as.integer(digits)
d #[1] 0 1 2 3 4 5 6 7 8 9
##  [1] 0 1 2 3 4 5 6 7 8 9

There is a collection of functions of the form as.something() for either coercion from one mode to other, or for investing properties of one object to other.

3.2 Changing Length of an Object

An empty object may still have a mode. Let’s just say

e <- numeric() # creates a numric empty vector
e
## numeric(0)
# can make empty vector of any type 

When an object is created the new components can be added to it. By simply added the index value outside its previous range.

e[3] <- 17
e
## [1] NA NA 17
# 1st two values will be empty just because we have assigned one and that one value will be the 3rd one.

To truncate the size of an object requires only an assignment to do so.We have an object d of length 10. To make it an object of length 5 consisting of just former components with even index in following way:

d <- d[2 * 1:5]
d
## [1] 1 3 5 7 9

We can retain the first 3 values by

length(d) <- 3
d
## [1] 1 3 5

3.3 Getting and Setting Attriutes

The function attributes(object_name) can be used to get all the non-instrinsic elements of an object.And the function attr(object, name) can be used to select a specific attribute.

3.4 Class of an Object

All objects in R always belongs to some class which can be reported by class function.