You are on page 1of 6

Example 1 Example 2

Suppose we have some data (10 integers from 0 to 9) and we Here, we can supply a vector for each dimension to extract a subset
need to arrange them in three dimensions: 1 for the first of an array:
dimension, 5 for the second, and 2 for the third: a1[1,,]
a1 <- array(c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), dim = c(1, 5, 2)) ## k1 k2
## c1 0 5 a1[, 2,]
a1
## c2 1 6 ## k1 k2
## , , 1 ## c3 2 7 ## 1 6
## ## c4 3 8
## c5 4 9
## [,1] [,2] [,3] [,4] [,5]

## [1,] 0 1 2 3 4 a1[1, 2:4, 1:2]


## k1 k2
## a1[,,1]
## c2 1 6
## , , 2 ## c1 c2 c3 c4 c5
## c3 2 7
## 0 1 2 3 4 a1[1, 1,
## ## c4 3 8
1]
a1[c("r1"), c("c1","c3"),"k1"]
## [,1] [,2] [,3] [,4] [,5] ## [1] 0
## c1 c3
## [1,] 5 6 7 8 9 ## 0 2

Slide 23 of 45 Slide 24 of 45

Using LIST object in R Using LIST object in R (Contd.)

A list is a generic vector that is allowed to include different types of Extracting an element from a list: There are various ways to access the
objects, even other lists. elements of a list. The most common way is to use a dollar-sign $ to
extract the value of a list element by name:
Creating a list : We can use list() to create a list, as the function name
l1 <- list(x = 1, y = c(TRUE, FALSE), z = c("a", "b", "c"), m =
suggests. Different types of objects can be put into one list.
NULL)
We can assign names to each list entry using named arguments: l1$x
l1 <- list(x = 1, y = c(TRUE, FALSE), z = c("a", "b", "c")) ## [1] 1
l1 l1$y
## $x
## [1] TRUE FALSE
## [1] 1
l1$z
##
## [1] "a" "b" "c"
## $y
l1$m
## [1] TRUE FALSE
## NULL
##
## $z Note: That if we ask for a non-existing element m, NULL will be returned.
## [1] "a" "b" "c"

Slide 25 of 45 Slide 26 of 45

1
Using LIST object in R (Contd.) Using LIST object in R (Contd.)

Subsetting a list: Other functions:


In many cases, we need to extract multiple elements from a list. These Many functions in R are related to lists. For example, if we are not
multiple members also construct a list as a subset of the original list. sure whether an object is a list or not, we can call is.list() to find out:
To subset a list, we can use single-square-bracket notation, just like l2 <- list(a = c(1, 2, 3), b = c("x", "y", "z", "w"))
what we use for vectors and matrices. We can extract some elements is.list(l2)
of a list and put them into a new list.
## [1] TRUE

is.list(l2$a)

## [1] FALSE

Here, l2 is a list, and but l2$a is a numeric vector rather than a list.

Slide 27 of 45 Slide 28 of 45

Using LIST object in R (Contd.) Using LIST object in R (Contd.)

In many we can also convert a vector to a list using as.list(): Other functions (contd.):
l3 <- as.list(c(a = 1, b =2, c = 3))

l3 It is also easy to coerce a list to a vector by calling unlist that basically


converts all list members and puts them to a vector of a compatible
## $a
type:
## [1] 1

##
l4 <- list(a = 1, b = 2, c = 3)
## $b
unlist(l4)
## [1] 2
## a b c
##
## 1 2 3
## $c

## [1] 3

Slide 29 of 45 Slide 30 of 45

2
Using LIST object in R (Contd.) Example 1

If we unlist a list of numbers and texts in mixture, all members will be For example, the following code creates a simple list that
converted to the closest type that each one can be converted to:
contains a single-element numeric vector, a two-entry logical
vector, and a character vector of three values:
l4 <- list(a = 1, b = 2, c = "hello")

unlist(l4) l0 <- list(1, c(TRUE, FALSE), c("a", "b", "c"))

## a b c l0

## "1" "2" "hello“ ## [[1]]

## [1] 1

##
Here, l4$a and l4$b are numbers and can be converted to a
## [[2]]
character; however, butl4$c is a character vector and cannot be
converted to numeric values. ## [1] TRUE FALSE

##
Therefore, their closest type that is compatible with all elements is
## [[3]]
a character vector.
## [1] "a" "b" "c"

Slide 31 of 45 Slide 32 of 45

Example 2 Example 2 (Contd.)

Example for extracting and subsetting in a list: The notation is very ## $x


much consistent with how it works for vectors. We can extract ## [1] 1
elements from a list by name using a character vector, or by position
l1[c(1, 2)]
using a numeric vector, or by criterion using a logical vector:
## $x
l1["x"]
## [1] 1
## $x
##
## [1] 1
## $y
l1[c("x", "y")]
## [1] TRUE FALSE
## $x
l1[c(TRUE, FALSE, TRUE)]
## [1] 1

##

## $y
To summarize, we can say that [[ means extracting one element
from a Vector or list, and [ means subsetting a vector or list. Sub
## [1] TRUE FALSE setting a vector will result in a vector. Likewise, subsetting a list will
l1[1] result in list.

Slide 33 of 45 Slide 34 of 45

3
Using DATAFRAMES object in R Example 1

A data frame represents a set of data with a number of rows and For example to create a data frame, we can call data.frame() and supply
columns. It looks like a matrix but its columns are not necessarily of the the data of each column by a vector of the corresponding type:
same type.
persons <- data.frame(Name = c("Ken", "Ashley", "Jennifer"),

Gender = c("Male", "Female", "Female"),

This is consistent with the most commonly seen formats of datasets: Age = c(24, 25, 23),
each row, or data record, is described by multiple columns of various Major = c("Finance", "Statistics", "Computer Science"))
types.
persons

## Name Gender Age Major


The given table is ## 1 Ken Male 24 Finance
an example that can Name Gender Age Major
## 2 Ashley Female 25 Statistics
be fully characterized
Ken Male 24 Finance
by a data frame. ## 3 Jennifer Female 23 Computer Science
Ashley Female 25 Statistics
Note that creating a data frame is exactly the same as creating a list. This is
Jennifer Female 23 Computer Science because, in essence, a data frame is a list in which each element is a vector and
represents a table column and has the same number of elements.

Slide 35 of 45 Slide 36 of 45

Example1 (Contd.) Example1 (Contd.)

Other than creating a data frame from raw data, we can also create it We can also create a data frame from a matrix with the same method:
from a list by calling either data.frame directly or as.data.frame: m1 <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, byrow =
FALSE)
l1 <- list(x = c(1, 2, 3), y = c("a", "b", "c"))
data.frame(m1)
data.frame(l1)
## X1 X2 X3
## x y
## 1 1 4 7
## 1 1 a
## 2 2 5 8
## 2 2 b
## 3 3 6 9
## 3 3 c
as.data.frame(m1)
as.data.frame(l1)
## V1 V2 V3
## x y
## 1 1 4 7
## 1 1 a
## 2 2 5 8
## 2 2 b
## 3 3 6 9
## 3 3 c Note that the conversion also automatically
assigns column names to the new data frame.

Slide 37 of 45 Slide 38 of 45

4
Example2 Example2 (Contd.)

Subsetting a data frame as a list: For example, we can use $ to The subsetting operator ([) allows us to use a numeric vector to
extract the values of one column by name, or use [[ to do so by extract columns by position, a character vector to extract columns
position: by name, or a logical vector to extract columns by TRUE and
FALSE selection:

df1$id df1[1] df1[1:2]

## [1] 1 2 3 4 5 ## id ## id level

df1[[1]] ## a 1 ## a 1 0

## [1] 1 2 3 4 5 ## b 2 ## b 2 2

## c 3 ## c 3 1

## d 4 ## d 4 -1

List subsetting perfectly applies to a data frame and also yields ## e 5 ## e 5 -3


a new data frame.

Slide 39 of 45 Slide 40 of 45

Example2 (Contd.) Using FUNCTIONS object in R

df1["level"] df1[c(TRUE, FALSE, TRUE)] A function is an object you can call. Basically, it is a machine with
## level ## id score internal logic that takes a group of inputs (parameters or
## a 0 ## a 1 0.5 arguments) and returns a value as output.
## b 2 ## b 2 0.2
## c 1 ## c 3 0.1 In the previous sections, we encountered some built-in functions
## d -1 ## d 4 0.5 of R. For example, is.numeric() takes an argument that can be
## e -3 ## e 5 0.9 any R object and returns a logical value that indicates whether
the object is a numeric vector. Similarly, is.function() can tell
whether a given R object is a function object.
df1[c("id", "score")]
## id score In fact, in R environment, everything we use is an object,
## a 1 0.5 everything we do is a function, and, maybe to your surprise, all
## b 2 0.2 functions are still objects. Even <- and + are both functions that
## c 3 0.1
take two arguments.
## d 4 0.5
## e 5 0.9

Slide 41 of 45 Slide 42 of 45

5
Example Solution

Example1: Solution1:
Function to convert from ° C to ° F using the following formula:

℉ = ℃ + 32 c2f<-function(c){
f=9/5*c +32}

Example2: Solution2:
Function to calculate compound interest when Principal, Rate and
time are given using the following formula:
𝑟 ci<-function(p,r,t){
𝑐𝑖 = 𝑝 1 + −1 c=p*((1+r/100)^t-1)}
100

Slide 43 of 45 Slide 44 of 45

You might also like