R – Objects
Every programming language has its own data types to store values or any information so that the user can assign these data types to the variables and perform operations respectively. Operations are performed accordingly to the data types.
These data types can be character, integer, float, long, etc. Based on the data type, memory/storage is allocated to the variable. For example, in C language character variables are assigned with 1 byte of memory, integer variable with 2 or 4 bytes of memory and other data types have different memory allocation for them.
Unlike other programming languages, variables are assigned to objects rather than data types in R programming.
Type of Objects
There are 5 basic types of objects in the R language:
Vectors
Atomic vectors are one of the basic types of objects in R programming. Atomic vectors can store homogeneous data types such as character, doubles, integers, raw, logical, and complex. A single element variable is also said to be vector.
Example:
# Create vectors x < - c( 1 , 2 , 3 , 4 ) y < - c( "a" , "b" , "c" , "d" ) z < - 5 # Print vector and class of vector print (x) print ( class (x)) print (y) print ( class (y)) print (z) print ( class (z)) |
Output:
[1] 1 2 3 4 [1] "numeric" [1] "a" "b" "c" "d" [1] "character" [1] 5 [1] "numeric"
Lists
List is another type of object in R programming. List can contain heterogeneous data types such as vectors or another lists.
Example:
# Create list ls < - list (c( 1 , 2 , 3 , 4 ), list ( "a" , "b" , "c" )) # Print print (ls) print ( class (ls)) |
Output:
[[1]] [1] 1 2 3 4 [[2]] [[2]][[1]] [1] "a" [[2]][[2]] [1] "b" [[2]][[3]] [1] "c" [1] "list"
Matrices
To store values as 2-Dimensional array, matrices are used in R. Data, number of rows and columns are defined in the matrix()
function.
Syntax:
matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
Example:
x < - c( 1 , 2 , 3 , 4 , 5 , 6 ) # Create matrix mat < - matrix(x, nrow = 2 ) print (mat) print ( class (mat)) |
Output:
[, 1] [, 2] [, 3] [1, ] 1 3 5 [2, ] 2 4 6 [1] "matrix"
Factors
Factor object encodes a vector of unique elements (levels) from the given data vector.
Example:
# Create vector s < - c( "spring" , "autumn" , "winter" , "summer" , "spring" , "autumn" ) print (factor(s)) print (nlevels(factor(s))) |
Output:
[1] spring autumn winter summer spring autumn Levels: autumn spring summer winter [1] 4
Arrays
array()
function is used to create n-dimensional array. This function takes dim attribute as an argument and creates required length of each dimension as specified in the attribute.
Syntax:
array(data, dim = length(data), dimnames = NULL)
Example:
# Create 3-dimensional array # and filling values by column arr < - array(c( 1 , 2 , 3 ), dim = c( 3 , 3 , 3 )) print (arr) |
Output:
,, 1 [, 1] [, 2] [, 3] [1, ] 1 1 1 [2, ] 2 2 2 [3, ] 3 3 3,, 2 [, 1] [, 2] [, 3] [1, ] 1 1 1 [2, ] 2 2 2 [3, ] 3 3 3,, 3 [, 1] [, 2] [, 3] [1, ] 1 1 1 [2, ] 2 2 2 [3, ] 3 3 3
Data Frames
Data frames are 2-dimensional tabular data object in R programming. Data frames consists of multiple columns and each column represents a vector. Columns in data frame can have different modes of data unlike matrices.
Example:
# Create vectors x < - 1 : 5 y < - LETTERS[ 1 : 5 ] z < - c( "Albert" , "Bob" , "Charlie" , "Denver" , "Elie" ) # Create data frame of vectors df < - data.frame(x, y, z) # Print data frame print (df) |
Output:
x y z 1 1 A Albert 2 2 B Bob 3 3 C Charlie 4 4 D Denver 5 5 E Elie
Please Login to comment...