Basic Modes
Learning objectives
All objects have two intrinsic attributes: mode and length. The mode is the basic type of the elements of the object. There are four main modes: numeric, character, complex, and logical (FALSE or TRUE).
Once the mode of an object is known, it they can be assembled into into more complex data structures (vectors, arrays, dataframes, etc.) that we will study later.
In this section you will learn about the modes you will be using for most econometric analyses.
Numerics
Numbers are stored as numerics in R.
You may want to create some numbers with decimal values. It is the default computational mode for numbers. However, sometimes you will want to create integer values (i.e. to store values such number of kids in a household).
If you assign a decimal value to a variable $ x $, it will be of numeric mode. Note that, even if we assign an integer to a variable $ y $, it is still being saved as a numeric value.
(x <- 10.5) # assign a decimal value
## [1] 10.5
mode(x) # print the mode of x
## [1] "numeric"
y <- 1
mode(y)
## [1] "numeric"
Integers
If you want to work with integers and not real numbers, you have to indicate it clearly. This is done by creating an additional attribute indicating it is an integer.
There are two ways to do this. The first one is to add an “L” after the number. The second one is to use the function as.integer()
.
If you want to check, whether the number you have stored is treated as an integer, you can use the function is.integer()
.
(z <- 3L) # declare an integer by appending an L suffix
## [1] 3
is.integer(z)
## [1] TRUE
(z <- as.integer(3)) # use the as.integer( ) function
## [1] 3
is.integer(z)
## [1] TRUE
Note that the function as.integer
can coerce a real number or even some character variables into an integer value.
(z <- as.integer(3.14))
## [1] 3
is.integer(z)
## [1] TRUE
(z <- as.integer("5.27")) # coerce a decimal string
## [1] 5
is.integer(z)
## [1] TRUE
as.integer()
will also be useful to transform a variable into an integer.
z <- 3.2
zL #adding L after the name of the variable will not work
## Error in eval(expr, envir, enclos): object 'zL' not found
z <- 3.2
int_z <- as.integer(z)
mode(int_z)
## [1] "numeric"
is.integer(int_z)
## [1] TRUE
Note that the mode is still “numeric”, but you have added an additional attribute saying it is an integer.
Arithemic operators with numerics and integers
Type the following in your console
(x <- 1L + 2)
## [1] 3
mode(x)
## [1] "numeric"
(y <- 1L + 2L)
## [1] 3
mode(y)
## [1] "numeric"
In the first case (x), you are asking R to add-up an integer and a real number. R cannot do that immediately since they are objects of different types. However, R does not throw an error. Instead, R will first convert (coerce) the integer into a numeric, and then make the addition between two numerics and output a numeric.
Again, R is very helpful and make this operation transparent to you. However, if you really expect an integer as a result, you need to be extra careful.
Logicals
A logical value is either TRUE
or FALSE
A logical is often created via comparison between variables.
x <- 1; y <- 2 # sample values
(z <- x > y) # is x larger than y?
## [1] FALSE
class(z)
## [1] "logical"
Standard logical operations are “&” (and), “|” (or), and “!” (negation).
u <- TRUE; v <- FALSE
u & v # u AND v
## [1] FALSE
u | v # u OR v
## [1] TRUE
!u # negation of u
## [1] FALSE
Further details and related logical operations can be found when typing help("&")
Finally, it is often useful to perform arithmetic on logical values. You just have to remember that the TRUE
has the value 1, while FALSE
has value 0.
(as.integer(TRUE)) # is k an integer?
## [1] 1
TRUE + TRUE
## [1] 2
Characters
In R, strings are stored as a character
object. Strings are surrounded by two "
.
(x <- "This is a string")
## [1] "This is a string"
class(x)
## [1] "character"
If you do not use the "
, R will look for a variable instead of a string of characters, and will most likely throw an error message.
(x <- This is a string)
class(x)
## Error: <text>:1:12: unexpected symbol
## 1: (x <- This is
## ^
We can convert objects into character values with the as.character()
function:
(x = as.character(3.14))
## [1] "3.14"
Two character values can be concatenated with the paste()
function.
fname = "Joe"; lname ="Smith"
paste(fname, lname)
## [1] "Joe Smith"
To extract a substring, we use the substr()
function. Here is an example showing how to extract the substring between the third and fourteenth positions in a string.
substr("Mary has a little lamb.", start=3, stop=14)
## [1] "ry has a lit"
And to replace the first occurrence of the word “little” by another word “big” in the string, we apply the sub()
function.
sub("little", "big", "Mary has a little lamb.")
## [1] "Mary has a big lamb."
Other Objects
R also provides special objects that are useful for missing data, or for specific number like Infinity.
NA is in fact a logical constant of length 1 which contains a missing value indicator. It will appear in your datasets and answers, when a value is missing.
class(NA)
## [1] "logical"
NULL represents the null object in R. NULL is returned by expressions and functions whose value is undefined. Contrary to NA, it is not a logical variable.
class(NULL)
## [1] "NULL"
Inf
and -Inf
are positive and negative infinity. Their class is numeric
1+ Inf
## [1] Inf
class(Inf)
## [1] "numeric"
NaN is a reserved word in R. Although it means “Not a Numeric”, it belongs also the class numeric.
class(NaN)
## [1] "numeric"
# See the difference between the two expressions
3 / 0 ## = Inf a non-zero number divided by zero creates infinity
## [1] Inf
0 / 0 ## = NaN # 0/0 is not a valid operation
## [1] NaN
Exercises
Exercise 1
Suppose you have typed this command,
x <- 2L + TRUE
y <- 3 + FALSE
- what is the value of x? what is be the mode of x? Is x an integer?
- what is the value of y? what is the mode of y? Is x an integer?
Before answering this question, try to imagine in what mode some elements may have been coerced by R?
Show the result
x <- 2L + TRUE
x
## [1] 3
mode(x)
## [1] "numeric"
is.integer(x)
## [1] TRUE
y <- 3 + FALSE
y
## [1] 3
mode(y)
## [1] "numeric"
is.integer(y)
## [1] FALSE
Exercise 2: Include " in a character variable
A value of mode character is input with double quotes “. It is possible to include this character in the variable if it follows a backslash . The two charaters altogether " will be treated in a specific way by some functions such as cat for display on screen.
- Create a variable x that contains the string “Double quotes " delimitate R’s strings.”
- Consult the help file of the functions print() and cat()
- See the difference between print(x) and cat(x)
- Using the function
sub()
, create a new variabley
from the variable x where you eliminate the two characters " - use the function cat to display y
Show the solution
x <- "Double quotes \" delimitate R’s strings."
print(x)
## [1] "Double quotes \" delimitate R’s strings."
cat(x)
## Double quotes " delimitate R’s strings.
y <- sub(" \"","", x)
cat(y)
## Double quotes delimitate R’s strings.
Exercise 3
Suppose you have typed this command,
x <- as.numeric(2L) + 3L
Tip: Instead of using as.integer()
, we used as.numeric()
. Check the help file of as.numeric before answering the question.
What is the mode of x?
Show the solution
x <- as.numeric(2L) + 3L
mode(x)
## [1] "numeric"