March, 2018

List

Create list

  • List can be understood as object chain that can have different types of data objects.
j <- list(names='Joe', salary = 55000, union = TRUE)
  • The object j has a list with length 3. It has three different data object, character vector, numeric vector, and logical vector.
  • The nodes have their name, names, salary, and union.

Create list

jn <- list('Joe', 55000, TRUE)
  • List can be defined without names of nodes.
  • List can be initialized without any data object.
jini <- vector(mode = 'list', length = 10)

Call the components in the List

  • The components in the list can be call by the name.
j$salary
## [1] 55000
j[["salary"]]
## [1] 55000
j[[2]]
## [1] 55000

Add components in the list

j$history <- 1:10
j[[1]] <- "Yoon"
j
## $names
## [1] "Yoon"
## 
## $salary
## [1] 55000
## 
## $union
## [1] TRUE
## 
## $history
##  [1]  1  2  3  4  5  6  7  8  9 10

Factor

Factor

x <- c(5, 12, 13, 12)
xf <- factor(x)
xf
## [1] 5  12 13 12
## Levels: 5 12 13
  • Factor is the interesting data type applied to the discrete data analysis.
  • The level is the unique value of Factor. It is useful for dummy coding in the categorical data analysis.

Structyre of factor

str(xf)
##  Factor w/ 3 levels "5","12","13": 1 2 3 2
unclass(xf)
## [1] 1 2 3 2
## attr(,"levels")
## [1] "5"  "12" "13"
  • The factor is an ordered integer valued vector having a name (level).
as.numeric(xf)
## [1] 1 2 3 2

Defining levels in factor

xff <- factor(x, levels = c(5, 12, 13, 88))
xff
## [1] 5  12 13 12
## Levels: 5 12 13 88

table function

x1 <- c(4,2,3,3,2,2)
table(x1)
## x1
## 2 3 4 
## 3 2 1
x2 <- c("a","b","a","a","b","b")
table(x2)
## x2
## a b 
## 3 3
x3 = data.frame(x1 = x1, x2 = x2)
table(x3)
##    x2
## x1  a b
##   2 0 3
##   3 2 0
##   4 1 0

Casting

class of objects

type of elements

  • vector and matrix only have the elements with the same type.
  • dataframe and list have the elements with the different type

type (class) of objects

  • A p-dimensional column vector is a \(n\times p\) matrix, and the vise versa is also true.
  • Dataframe and List has the common ways to refer their elements.

class of objects

Dataframe and matrix

rbind and cbind

A = data.frame(x1 = rep(0,2), x2 = rep('b',2))
B = data.frame(x3 = rep(1,2), x2 = rep('d',2))
AB = cbind(A,B)
head(AB)
  • rbind(A,B) does not work. Why?

class of objects

type (class) of objects

  • The changing task of class or the type of element is called 'casting'
  • Casting is sometimes automatically done in R
a = 0L
a[2] = 1
typeof(a[1])
## [1] "double"

class of objects

type (class) of objects

  • The changing task of class or the type of element is called 'casting'
  • Casting is sometimes automatically done in R
a = matrix(1:10,5,2)
b = a[,-1]
class(b)
## [1] "integer"

class of objects

type (class) of objects

  • The changing task of class or the type of element is called 'casting'
  • Casting is sometimes automatically done in R
a = matrix(1:10,5,2)
b = a[,-1, drop = FALSE]
class(b)
## [1] "matrix"

class of objects

casting function (class)

  • matrix \(\rightarrow\) vector: c()
a = matrix(1:10,5,2)
b = c(a)
str(b)
##  int [1:10] 1 2 3 4 5 6 7 8 9 10

class of objects

casting function (class)

  • vector \(\rightarrow\) factor: as.factor()
a = c("tommy", "jimmy", "jane")
b = as.factor(a)
b
## [1] tommy jimmy jane 
## Levels: jane jimmy tommy
# factor -> integer vector
c(b)
## [1] 3 2 1

class of objects

casting function (class)

  • list or data.frame \(\rightarrow\) vector: unlist()
a = list()
for (i in 1:5) a[[i]] = i
b = unlist(a)

class of objects

casting function (class)

  • vector \(\rightarrow\) matrix: as.matrix() (=column vector)
a = 1:3
b = as.matrix(a)
b
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    3

class of objects

casting function (class)

  • matrix \(\rightarrow\) data.frame: as.data.frame()
a = matrix(1:10,5,2)
b = as.data.frame(a)
str(b)
## 'data.frame':    5 obs. of  2 variables:
##  $ V1: int  1 2 3 4 5
##  $ V2: int  6 7 8 9 10

class of objects

casting function (class)

  • data.frame \(\rightarrow\) list: unclass()
a = matrix(1:10,5,2)
b = as.data.frame(a)
b = unclass(b)
class(b)
## [1] "list"
  • Actually dataframe is a list object with double array indices.

class of objects

casting function (type of elements)

  • as.character()
  • as.numeric() = as.double()