Professional Documents
Culture Documents
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]
Slide 23 of 45 Slide 24 of 45
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.)
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
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))
##
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")
## a b c l0
## [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
##
## $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"),
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
Slide 35 of 45 Slide 36 of 45
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:
## [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
Slide 39 of 45 Slide 40 of 45
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