Introduction to R Data Types
R data types are the basic features of R language that are used to accept and store various types of data. R language supports commonly used primitive or Scalar data types such as char, integer, double, logical and complex data types that accept and process as single data value at a time. Also, R language supports vectors that can hold similar data types of one dimensional. The matrix data types, list, data frames, arrays are for two or multidimensional data formats. There are built-in keywords and functions available in the R language to declare each data type and assign values to the variable related to the data types.
Explain R Data Types
R programming supports various datatypes like scalars, matrices, lists, vector,s and data frames. Everything in R is considered an object, which means it stores and processes operations on objects). The key feature of R is a different process is done with different types of objects. Most of the commands in R involves applying functions to the objects. Variables do not require a declaration, instead of assigning a sequence of numbers to the vectors can be done.
Let’s learn the types one by one:
Vector has a set of values with the same types (collection of ordered elements) represented in one dimensional. The class of the vector is determined by the type of the entries made. When a vector is created for more than one element, the c () function is used to concatenate all the elements together in a single vector. Vectors are a string of numeric, sequential numbers or random numbers. Vector varieties are the character, integer, numeric, complex, logical (true, false). These are implicit conversion. Some of the functions fives vector functions are length(), class(x), is.logical(x) , is.null , rep().
In the below, we can see basic vector examples:
- Vector Arithmetic: Numeric vectors are performed in arithmetic expressions to do calculations to give another vector. Statistical operations also are done, which gives entries like max, min, var mean.
y <-c (1, 2 ,2.5 ,3)
The above statement gives the output by using the c() function, which adds variable t to 2.
- The length of the vector is calculated by the len () function.
- Logical Vectors: Comparison of two numbers with logical values like True, false, NA. Logical operators to satisfy certain conditions include <, <=, >, >=, ==, != for inequality.
v <- seq ( -2 ,2)
l < -v > 0
rep () – to create replicate values.
rep( 3:6 ,2)
rep( 1:3 , each =2)
rep(1:3 , times=2 ,each =2)
- Create a Vector
color <- c (‘blue’, ‘pink’, ‘white’)
- To display the class of the vector
print ((class (color))
In the above program , this one denotes the first element of the vector.
The factor adds numeric codes along with the character level. In simple, it defines categorical data with ordered and unordered sets. They are defined using function factor (). Storing data in a factor helps to store data efficiently in statistical modeling.
f = factor (c(1, 6,2,4,7,1,6,7,8)
k = factor (c( 2,0,2,0,0,0 ) , levels =c(0,2) , labels =c( “ prince “ ,”princess”))
In R programming matrix is a two-dimensional element with numeric and character vectors, simply an atomic vector with the number of rows and columns. Three ways to create a matrix are using the function matrix(), converting the vector into the matrix, and binding vectors. Some functions useful here are :
- rbind() and cbind(): combines or binds columns and rows.
- dim(): setting dimensions.
variable <- matrix(vector, n rows, n columns, split by row or column)
Here if it is true, it splits by row, false returns split by columns.
- Consider a matrix.
x = matrix(c (1,2,3,4,5,6,7,8) 2,4, true)
- Considering Bind.
a <- 1:4
b<- 10 :13
List stores Objects, and the elements can be a character, matrices, arrays, numeric. it may consist of another list as an item too.
variable <- list (list items)
Example of an R list:
lak = list (23, “hi”, cos, list (5L,” l”))
Example Considering copies of three Vectors:
x=c (true, false, true)
Therefore y holds the copies of a,b,x.
5. Data Frame
Data frames are two dimensional with a group of vectors by an equal length. It’s a special kind of list with a rectangular format list. The key factor is to store data tables. They are created using function data. the frame ().
variable <- data.frame ( list 1, list 2… list N)
Let’s see an example of the data frame In R.
X= data.frame( values =c(20,50,10), name =c(‘ Gri’,’Tom’,’jeff’))
print(X) values Name
Even we can use built-in data frames. In which the top element defines a header, followed by data rows and columns. To see the preview, we can use the head function before.
Date intel speed data
hp 1990 8081 MHZ 8
acer 2001 80286 Mhz 16
To define the class of the intel:
In this article, we have gone through different R data types that are used in programming. To make any application, we need variables to store the values, and all these variables are necessary to assign data types. These data types are used in data analysis. Understanding data types help while debugging for computational purposes.
This is a guide to R Data Types. Here we discuss different types of R data with various examples to assign data types. You can also go through our other related articles to learn more –