A Appendix A
A.1 Basic Data Structure in R
Though in the book, most of the time we talk about data frames (sometimes tibble), vectors are the buliding blocks of them. Traditional learners of R usually start learning it using vectors. However, for accountants (or would-be accountants) it is better to begin with tibbles and work down to the underlying concepts such as vectors.
Basically there are two types of vectors in R; one is called Atomic vectors (also called homogeneous vectors) and the other is called lists. Atomic vectors contain similar types of elements, while lists vectors do not; lists can hold differnet types elements in a vector. There are six types of Atomic vectors - logical, integer, double, character, complex, and raw. The integerand double are together called numeric. Of these types, the first four are widely used and most relevant for accounting analytics.
Each vector has 2 major characteristis - type and length. The function typeof can be used to know about the types of the vectors - namely logical, integer, double, character, complex, or raw. The length function is used to get or set the length of a vector. The function nchar can be used to get the length of a string. Some examples of vectors are given below -
# Character type
a <- c("1933","1934","2002")
length(a)## [1] 3
typeof(a)## [1] "character"
a## [1] "1933" "1934" "2002"
b <- c("The Securities Act", "The Securities Exchange Act", "Sarbanes-Oxley Act")
length(b)## [1] 3
typeof(b)## [1] "character"
nchar(b)## [1] 18 27 18
b## [1] "The Securities Act"
## [2] "The Securities Exchange Act"
## [3] "Sarbanes-Oxley Act"
# Logical type
c <- c(TRUE, FALSE, TRUE, TRUE, TRUE, FALSE)
length(c)## [1] 6
typeof(c)## [1] "logical"
c## [1] TRUE FALSE TRUE TRUE TRUE FALSE
# Double type
d <- c(1933, 1934, 2002)
length(d)## [1] 3
typeof(d)## [1] "double"
d## [1] 1933 1934 2002
# Integer type
e <- c(1933L, 1934L, 2002L)
length(e)## [1] 3
typeof(e)## [1] "integer"
e## [1] 1933 1934 2002
f <- list(a,b,c,d,e)
length(f)## [1] 5
typeof(f)## [1] "list"
f## [[1]]
## [1] "1933" "1934" "2002"
##
## [[2]]
## [1] "The Securities Act"
## [2] "The Securities Exchange Act"
## [3] "Sarbanes-Oxley Act"
##
## [[3]]
## [1] TRUE FALSE TRUE TRUE TRUE FALSE
##
## [[4]]
## [1] 1933 1934 2002
##
## [[5]]
## [1] 1933 1934 2002
Very good website - https://rc2e.com/somebasics#recipe-id017