Data Types with Examples in R Language

R Language | Data Types: In this tutorial, we are going to learn about the various data types in R programming language with examples.
Submitted by Bhavya Sri Khandrika, on May 02, 2020

Traditionally, while programming in any programming language the programmer needs to take the help of several available variables to store the distinct information in them. When the variables are considered, in simple terms, they are defined as the store places for storing the data of various types. The variables are generally used to store the data as they possess the reserved memory locations to preserve the values. In simple words, we can define the variables as the memory locations which when created reserve some space for storing some values in them.

Fine, we learned the fact that the variables are commonly used to store the data. But we humans wish to store data of various types like sometimes characters, integers, wide characters, floating points, Double floating points, long integers, unsigned numbers, signed numbers, boolean, etc. Now based on our requirements we need to access the different types of data types. Now based on the type of data type the operating system takes the responsibility of allocating the memory to the variables and decides on the matters like which data must be stored into the reserved memory.

In contrast to the other programming languages like the C and the Java, in the R language, the variables are not declared with the data types. Here in the R language, the R objects are used for the declaration of the variables. Hence the data type of the declared R object becomes the data type of the considered variable. The data objects that are included in the R languages are many. Therefore on attempting to list out some of them, we obtain the below list:

List of Data Types

  1. Vectors
  2. Arrays
  3. Lists
  4. Factors
  5. Matrices
  6. Data frames

Among the above objects, the simplest of all of them is a vector object. Moreover, there are other six atomic objects which are mainly useful in building the R objects. They are,

  1. Logical
  2. Integer
  3. Numeric
  4. Character
  5. Complex
  6. Raw

Thus, as stated earlier, working with the vectors is the simplest way that makes the user very comfortable while coding. The vectors consist of the above-mentioned classes. One of the specialties of the R language is that the digit of classes is not constrained to merely above the six categories. Since they are numerous types, which are used by the programmer based on the requirements in the code.

Before the article, we have seen the different types of data types that are exclusively available in the R language. In simple words, the data types are assigned to the variables. Thus, these data types give a command to the operating system to store the data that matches the declared data type. The variables are ought to store the particular data which resembles the declared data types while the declaration of variables is done. But here we need to note an important point that in the R language the programmer needs to store the data in a variable with the help of objects that are available in the R language. Thus, through this we can make sure that the variables declared in the R language are assigned with the help of R objects. Whatever the R object is assigned to a variable then that becomes the data type of that particular variable. However, there are many more R objects available but the usage of them mainly depends on the requirement in the program.

Some common data structures and their workings,

1) Vectors

Sometimes we require a variable such that we need to accommodate several items under the same attribute. In such a case, we opt for these vectors. Whenever one likes to create a vector that encompasses more than a single element in them then one should use the c() function. This function stands responsible for combining elements into a declared variable which is of type vector.

Example:

# Create a vector.
apple <- c('red','green',"yellow")
print(apple)

# Get the class of the vector.
print(class(apple))

Output

[1] "red"    "green"  "yellow"
[1] "character"

2) Matrices

Usually, the matrix is a 2-dimensional data set that takes the shape of a rectangle. The matrix variable is created with the help of vector input to the desired matrix function.

Example:

# Create a matrix.
M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE)
print(M)

Output

     [,1] [,2] [,3]
[1,] "a"  "a"  "b" 
[2,] "c"  "b"  "a" 

3) Lists

In general, the word list gives us the impression that it contains several items of either the same category or different types In a similar fashion, the Lists in the R language also contain the various types of elements like function, vectors and sometimes there will be a list present in a single list. The below code is an example of the demonstrations of the concept of lists in the R language.

Example:

# Create a list.
list1 <- list(c(2,5,3),21.3,sin)
# Print the list.
print(list1)

Output

[[1]]
[1] 2 5 3

[[2]]
[1] 21.3

[[3]]
function (x)  .Primitive("sin")

Here the above text has dealt with some of the data types that are mainly used in the R language.

4) Arrays

Arrays can possess any number of dimensions rather than that of the matrix. The arrays function in the R language considers the dim attribute that eventually creates the number of dimensions based on the user request.

The below example creates an array with the help of two elements which are arranged in the form of 3×3 matrices.

Example:

# Create an array.
a <- array(c('green','yellow'),dim = c(3,3,2))
print(a)

Output

, , 1

     [,1]     [,2]     [,3]    
[1,] "green"  "yellow" "green" 
[2,] "yellow" "green"  "yellow"
[3,] "green"  "yellow" "green" 

, , 2

     [,1]     [,2]     [,3]    
[1,] "yellow" "green"  "yellow"
[2,] "green"  "yellow" "green" 
[3,] "yellow" "green"  "yellow"

5) Factors

The factors are also created by using vectors only. The only difference is that the factors store the vectors along with the different values of the elements in the created variable that resembles a vector. The values in the vectors are stored as labels. However, the vectors are perpetually characters irrespective of the type they take up as they may be numeric, boolean, or character. But indeed they are taken as a character in the vector considered. These factors are useful in Statistical modeling. The function factor() is used for creating the factors. The nlevels is the function which enables the programmer or the user to count the levels available in the particular program.

The following code will help you in understanding the above statements very clearly.

Example:

# Create a vector.
apple_colors <- c('green','green','yellow','red','red','red','green')

# Create a factor object.
factor_apple <- factor(apple_colors)

# Print the factor.
print(factor_apple)
print(nlevels(factor_apple))

Output

[1] green  green  yellow red    red    red    green 
Levels: green red yellow
[1] 3

6) Data Frames

Generally, the data frames are known to be the tabular data objects that fall under the above category. In these data frames, each frame consists of various modes of values. For example, the first column can be of numeric type, and then the next one may be of a character and so on.

The function which enables the user to create a data frame is the data.frame() function.

Example:

# Create the data frame.
BMI <-   data.frame(
   gender = c("Male", "Male","Female"), 
   height = c(152, 171.5, 165), 
   weight = c(81,93, 78),
   Age = c(42,38,26)
)
print(BMI)

Output

  gender height weight Age
1   Male  152.0     81  42
2   Male  171.5     93  38
3 Female  165.0     78  26


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.