2.1 Data Type
There are five basic data types:
- logical
- integer
- numeric
- complex
- character.
2.1.1 Logical
Logical data type is binary : it is either TRUE or FALSE.
## [1] TRUE
## [1] FALSE
TRUE and FALSE correspond to integer 0 and 1. And we can use it for calculation. Say \(x+x+x\) should give us 3:
## [1] 3
And \(x \times y\) should give us zero:
## [1] 0
One important logical data besides TRUE and FALSE is NA. It is usually used to denote missing data. One intesting feasture of NA is that any operation on it will give you the same result.
## [1] NA
## [1] NA
As we shall see in the next chapter, NA can be a problem because TRUE and FALSE is needed for conditional statement (e.g., if-statement).
To see what datat type of a variable is, we can use the function class().
## [1] "logical"
2.1.2 Integer
Integer data type covers all whole numbers. It is represented by number and letter L: 1L, 2L, 3L.
## [1] 1
## [1] "integer"
2.1.3 Numeric
Numeric data type covers all real numbers.
## [1] 2.6
## [1] "numeric"
2.1.4 Complex
Complex data type covers all complex numbers but this is rarely used in financial analysis.
## [1] 2+5i
## [1] "complex"