# -R object

#### **There are many types of R-objects. The frequently used ones are Vectors, Matrices, Arrays, Data frame, List, and Factor** <a href="#there-are-many-types-of-r-objects-the-frequently-used-ones-are-vectors-matrices-arrays-data-frame-li" id="there-are-many-types-of-r-objects-the-frequently-used-ones-are-vectors-matrices-arrays-data-frame-li"></a>

#### Vectors: <a href="#vectors" id="vectors"></a>

&#x20;A vector is a sequence of same datatype elements

```r
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)]

```r
 b[c(1,2)] # 1st and 2nd elements of vector b
```

#### Matrix: <a href="#matrix" id="matrix"></a>

&#x20;A matrix is two-dimensional vector. The matrix elements must be of the same data type

```r
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: <a href="#array" id="array"></a>

An array is matrix with more than 2-dimensions (n-matrix)

```r
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 href="#data-frame" id="data-frame"></a>

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

```r
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 href="#list" id="list"></a>

&#x20;A list is a vector containing other objects. Every object can be different length and datatype

```r
x<-c(1:10)
y<-c(TRUE,FALSE)
z<-c("cevi","Julia","Marissa")
thelist<-list(x,y,z)
print(thelist)
############
[[1]] 
[1]  1  2  3  4  5  6  7  8  9 10​
[[2]] 
[1]  TRUE, FALSE
​[[3]][1] "cevi"    "Julia"   "Marissa"​
```

#### Factor and Order <a href="#factor-and-order" id="factor-and-order"></a>

Factor create a distinct value from the data and ordered() create a ordinal structure of the data (based on alphabetical or numerical value)

```r
gender <-c("male","male","female")
genderfactor(gender)
##############
[1] male   male   female
Levels: female male

​#ordered()
rating<-c(1,2,2,3,4,5,6,6,6,4)
ordered(rating)
################
[1] 1 2 2 3 4 5 6 6 6 4
Levels: 1 < 2 < 3 < 4 < 5 < 6
```
