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 integer
and 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
<- c("1933","1934","2002")
a length(a)
## [1] 3
typeof(a)
## [1] "character"
a
## [1] "1933" "1934" "2002"
<- c("The Securities Act", "The Securities Exchange Act", "Sarbanes-Oxley Act")
b 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(TRUE, FALSE, TRUE, TRUE, TRUE, FALSE)
c length(c)
## [1] 6
typeof(c)
## [1] "logical"
c
## [1] TRUE FALSE TRUE TRUE TRUE FALSE
# Double type
<- c(1933, 1934, 2002)
d length(d)
## [1] 3
typeof(d)
## [1] "double"
d
## [1] 1933 1934 2002
# Integer type
<- c(1933L, 1934L, 2002L)
e length(e)
## [1] 3
typeof(e)
## [1] "integer"
e
## [1] 1933 1934 2002
<- list(a,b,c,d,e)
f 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