## Combining Objects

The c function combines vectors and lists

c(1, 2, 3)

## [1] 1 2 3

x <- 1:3; y <- 101:103
c(x, y)

## [1]   1   2   3 101 102 103

iris$Species[1:8]  ## [1] setosa setosa setosa setosa setosa setosa setosa setosa ## Levels: setosa versicolor virginica  The cbind and rbind functions can be used to append columns and rows, respecively. ma <- cbind(x, y) ma  ## x y ## [1,] 1 101 ## [2,] 2 102 ## [3,] 3 103  rbind(ma, ma)  ## x y ## [1,] 1 101 ## [2,] 2 102 ## [3,] 3 103 ## [4,] 1 101 ## [5,] 2 102 ## [6,] 3 103  ## Accessing Dimensions of Objects Length and dimension information of objects length(iris$Species)

## [1] 150

dim(iris)

## [1] 150   5


## Accessing Name Slots of Objects

Accessing row and column names of 2D objects

rownames(iris)[1:8]

## [1] "1" "2" "3" "4" "5" "6" "7" "8"

colnames(iris)

## [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"


Return name field of vectors and lists

names(myVec)

##  [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X"
## [25] "Y" "Z"

names(myL)

## [1] "name"        "wife"        "no.children" "child.ages"


## Sorting Objects

The function sort returns a vector in ascending or descending order

sort(10:1)

##  [1]  1  2  3  4  5  6  7  8  9 10


The function order returns a sorting index for sorting an object

sortindex <- order(iris[,1], decreasing = FALSE)
sortindex[1:12]

##  [1] 14  9 39 43 42  4  7 23 48  3 30 12

iris[sortindex,][1:2,]

##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 14          4.3         3.0          1.1         0.1  setosa
## 9           4.4         2.9          1.4         0.2  setosa

sortindex <- order(-iris[,1]) # Same as decreasing=TRUE


Sorting multiple columns

iris[order(iris$Sepal.Length, iris$Sepal.Width),][1:2,]

##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 14          4.3         3.0          1.1         0.1  setosa
## 9           4.4         2.9          1.4         0.2  setosa


Previous Page                     Next Page