In R the variables are not declared as data type but as objects. The data type of the R-object becomes the data type of the variable.
There are many types of R-objects. The frequently used ones are Vectors, Matrices, Arrays, Data frame, List, and Factor
Vectors:
A vector is a sequence of same datatype elements
a <-c(1,2,3,4.9,10,2,4) # numeric
vectorb <-c("one","two","three") # character
vectorc <-c(TRUE,FALSE) #logical vectora;b;c
Get the position of the elements using vector[c(postion)]
b[c(1,2)] # 1st and 2nd elements of vector b
Matrix:
A matrix is two-dimensional vector. The matrix elements must be of the same data type
c<- matrix(
c(1:6), # the data elements
nrow=2, # number of rows
ncol=3, # number of columns
byrow = TRUE) # fill matrix by rows
c # print the matrix
#####################
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
Array:
An array is matrix with more than 2-dimensions (n-matrix)
a<-array(
c(1:9), # the data elements
dim=c(3,3,2) # 2-matrix from 3 row and 3 column )
a #print array a
#########################
, , 1
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
, , 2
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
# Get the position of the elements using array[c(postion)]
a[c(1,8)]
##########
[1] 1 8
Data frame:
A data frame is tabular store system. The column must be same length. Data frame used as the by most of R's modeling software
name<-c("cevi","Julia","Marissa")
class<-c(1,2,3)
note<-c(1,1.7,2.3)
student<-data.frame(name,class,note)
print(student)
##############
name class note
1 cevi 1 1.0
2 Julia 2 1.7
3 Marissa 3 2.3
List:
A list is a vector containing other objects. Every object can be different length and datatype