Data Types — R Objects and Attributes
By: Kurt F.
Tags: Software Development, Programming, R Programming, R, Data

Data Types — R Objects and Attributes

R Programming objects and attributes

Photo by Franki Chamaki on Unsplash

In this article we’ll cover an important issue for R. We will talk about the different types of data used in R. We will also learn about some basic operations on data types.

If you’re ready, let’s get started:

Objects

Everything you manipulate in R, everything we encounter in R, objects that can be called objects, objects can all be of different types, contain all different types of data. But everything in R is an object. So R has five basic classes of atomic objects. These are fairly low levels or basic object classes, character, numeric. So these are like real numbers or decimal numbers, integers, complex numbers, and logicals

The most basic object in R is called a vector. One thing you cannot do with a standard vector is have mixed types of objects you cannot have a vector of characters and numerics, or numerics and integers, or integers and logicals. And everything in a vector has to be the same class.

Somtimes there’s one type of vector that can have multiple different types of classes. This is a “list”. So a list is represent as a vector, it’s a sequence of objects. But each element of that vector can be a different and ofcourse can be an object of a different class. You can have a list that’s inside the list and one element of the list can be a data frame so, any element of the list can be anything. You can be created directly from expressions using the quote mechanism and converted to and from lists by the as.list and as.call functions.

Also you can create an empty vector with the vector function. And the vector function has two basic arguments. The first argument is the class of the object, so the type of object that you want to have in the vector. And the second argument is the length of the vector itself. R has six basic (‘atomic’) vector types: logical, integer, real, complex, string (or character) and raw. The modes and storage modes for the different vector types are listed in the following table.

Numbers

Let’s come to the numbers as the most important object type. Numbers in R are generally treated as what are called numeric objectsum, so pretty much all numbers are treated as double number precision real numbers. So, even if you are looking at a number that’s like one or two, R thinks of those numbers as numeric objects there is a way to explicitly say you want an integer and you can specify the L subs, the L suf, the capital L suffix there. So for example, if you just enter the number 1 in R, that gives you a numeric object. But entering 1 with a capital L next to it explicitly gives you an integer. So, L specifies an integer type, rather than a double that the standard numeric class is.

> str(1)

num 1

> str(1L)

int 1

There’s a special inf, and inf is like a real number it can be used in calculations and you will get the expected result. So, for example, if you take one, divide it by zero, you’ll get infinity and if you take 1 and divide it by infinity you’ll get zero.

nan means "not a number", a float value that you get if you perform a calculation whose result can't be expressed as a number. Any calculations you perform with NaN will also result in NaN .

inf means infinity. For example:

>>> 2*float("inf")

inf

>>> -2*float("inf")

-inf

>>> float("inf")-float("inf")

nan

Attributes

Some of the most common types of attributes that we’ll encounter are names dimnames, dimensions, class, length, other user-defined attributes/ metadata. A dimension, so a matrix will have dimensions for example it will have a number of rows and a number of columns if you have a multidimensional array you’ll have more than two dimensions.

Every object have a class. So for example, numeric objects their class is numeric and integer objects, their class is integer.

studentBio <- list(studentName = "Tom Cruise", studentAge = 17, studentContact="New York") class(studentBio) <- "StudentInfo" studentBio $studentName

[1] "Tom Cruise"



$studentAge

[1] 19



$studentContact

[1] "New York"



attr(,"class")

[1] "StudentInfo" output:

$studentName

"Tom Cruise"



$studentAge

19



$studentContact

"New York"



attr(,"class")

"StudentInfo"

It is also necessary to know that every object has a length. Quite simply for a vector, the length of the object is the number of elements in the vector. There may also be other user-defined attributes or metadata that you can define individually for an object using various attribute functions. There is a generic function called attributes that allows you to set or change attributes() for an R object.