R Data Types
Data types are used to define the size and type of the variable. In R, there is no need to declare a variable as some data types. The variables are assigned with R-objects and the R-object’s data type becomes the variable’s data type.
There are mainly 6 R-objects:
- Vectors
- Lists
- Arrays
- Matrices
- Data frames
- Factors
Vectors
Vector is a sequence of data elements of the same type. It is a one-dimensional array. The simplest way to create a vector in R is to use the c() function.
Example:
a = c(1, 2, 4.2, -10, 3) #numeric vector b = c(‘red’, ‘green’, ‘blue’) #character vector c = c(TRUE, TRUE, FALSE, TRUE) #logical vector a b c
Note: You can use = or <- to assign the values to a variable.
Output:
When we execute the above script file, it will display the following output:
[1] 1.0 2.0 4.2 -10.0 3.0 [1] “red” “green” “blue” [1] TRUE TRUE FALSE TRUE
There are five classes of vectors:
- Numeric
- Integer
- Complex
- Logical
- Character
- Numeric Data Types
Decimal values are used for numeric data types. If you assign a numeric value for any variable x like given below, x will become a numeric type.
Example:
> x = 24.13 # assigns a decimal value to x > x # Print the value of x
Output:
[1] 24.13
If we print the class name of x then:
> class (x) #print the class name of x
Output will be:
[1] “numeric”
Furthermore, even if we assign an integer value to a variable y, it is still being saved as a numeric value.
> y = 100 # assigns a decimal value to y > y # Print the value of variable y [1] 100 > class(y) #print the class name of y [1] “numeric”
- Integer Data Types
To create an integer type of variable in R, you have to invoke the as.integer() function.
Example:
> y = as.integer(100) # assigns a decimal value to y > y #print the value of y
Output:
[1] 100
And if you check the class of y:
> class(y) #print the class of y
Output will be:
[1] “integer”
You can also append the L suffix to declare an integer.
Example:
> y = 100L > class(y)
Output:
[1] “integer”
- Complex data type
Complex value can be defined by the pure imaginary value ‘i’.
Example:
> k = 1 + 2i #creating a complex number > k
Output:
[1] 1+2i
And if you check the class of k:
> class(k) #print the class of k
Output will be:
[1] “complex”
- Logical Data Types
Logical value is mostly created via comparison variables.
Example:
> a = 13; b = 24 # assigning values > c = a > b # is larger than b? > c # print the logical value
Output:
[1] False
We can also check the class:
> v = TRUE > class(v)
Output will be:
[1] “logical”
- Character Data Types
Character object is used to enter or represent string values in R. To convert objects into character, we have to use the as.character() function within your code.
Example:
> g = as.character(24.13) > g #print the character string
Output:
[1] 3.14 And if we check the class of g: > class(g) #print the class of g
Output will be:
[1] “character”
You can also assign some string values:
Example:
> s = “Hello World!” #assign string value > s #print the value of s
Output:
Hello World!
Lists
List is a collection of R-objects which contains elements of different types like –strings, numbers, vectors and another list inside it.
Example:
> n = c(3, 1, 6) > s = c("ab", "bc", "ca", "db", "ec") > x = list(n, s, TRUE) > x
Output:
[[1]] [1] 3 1 6 [[2]] [1] "ab" "bc" "ca" "db" "ec" [[3]] [1] TRUE
Matrices
Matrices are the R-objects, in which the two-dimensional rectangular data set are arranged. A matrix can be built using the matrix() function.
Syntax: matrix(data, nrow, ncol, byrow, dimensions)
Where, data: - it is the input vector which will be the data elements of the matrix
nrow: - number of rows to be created
ncol: - number of columns to be created
byrow: - it is just a logical clue. If it is TRUE, then the input vector elements are arranged by row.
dimname: - it specifies the names assigned to the rows and columns.
Example 1:
> Mat <- matrix(c(1:9), nrow = 3, ncol = 3 ) > Mat
Output:
[,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9
Example 2:
> Mat <- matrix(c(1:9), nrow = 3, ncol = 3, byrow = TRUE) > Mat
Output:
[,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9
Arrays
Unlike matrices, arrays can be of any number of dimensions. It means they can store data in more than two dimensions. It also takes vectors as input. To create an array, it uses the dim parameter.
Example:
Let's create an array with two elements with 3X3 matrices each.
> vector1 <- c(100,200,300) > vector2 <- c(400,500,600,700,800,900) > result <- array(c(vector1,vector2),dim = c(3,3,2)) > result
Output:
, , 1 [,1] [,2] [,3] [1,] 100 400 700 [2,] 200 500 800 [3,] 300 600 900 , , 2 [,1] [,2] [,3] [1,] 100 400 700 [2,] 200 500 800 [3,] 300 600 900
Factors
Factors are the R data objects. They are used to categorize the data and store it as levels. They can store both integers and strings. Factors are used in data analysis for statistical modeling. factor() function is used to create factors.
Example:
> data <- c('Physics','Maths','Physics','Bio','Bio','Maths','Physics','Maths','Bio') > fdata <- factor(data) > fdata
Output:
[1] Physics Maths Physics Bio Bio Maths Physics Maths Bio Levels: Bio Maths Physics
Data Frames
A data frame is a table or a two-dimensional array like structure in which each column contains values of one variable (means the first column can be a character, the second column can be numeric and thirds column can be logical, etc.) and each row has one set of values from each column. To create a data frame, we have to use data.frame() function.
> std_id = c (1:4) > std_name = c('Nikita','Deep','Priya',"Pihu") > marks = c(87, 89, 72, 65) > std.data <- data.frame(std_id, std_name, marks) > std.data
Output:
std_id std_name marks 1 1 Nikita 87 2 2 Deep 89 3 3 Priya 72 4 4 Pihu 65Reference: https://data-flair.training/blogs/r-data-types/ https://www.statmethods.net/input/datatypes.html