Inside the computer, everything is stored as a number
But we can represent other things using numbers
Numbers are not the only thing we will handle
R has four basic data types:
We have already spoke about numeric data
Here we will talk about other data types
You can try these two commands in your computer
The results are two keywords: TRUE
and FALSE
All uppercase, no quotation marks
These are logical values
We can assign logical values, as we did with numbers.
[1] TRUE
[1] FALSE
Each assignment overwrites the old value
The old values of a
and b
are forgotten
Most of times we do not assign TRUE
or FALSE
directly
Instead, we get logical values as the result of a comparison
In this case we ask if one is less or equal than the other
The symbol <=
means “less than or equal to” (≤)
The symbol >=
means “greater than or equal to” (≥)
The symbols ==
and !=
correspond to = and ≠
Important: we use ==
to compare two numbers
We are asking a question
It should not be confused with =
, which is used for assignment, and is the same as <-
==
to ask if things are equalThe following code is correct
It asks “are these two numbers equal?”
The answer is FALSE
The special operator %in%
asks if
[1] TRUE
This corresponds to the question \[\text{Does } 1 ∈ \{3, 2, 1, 0\}\text{ ?}\] It is a question about sets
If we have these two variables
What is the result of these commands?
What do we do first here?
Shall we add or compare?
Comparisons come after sum and minus
Thus, the code is equivalent to
We can make logic vectors like we created numeric vectors
[1] TRUE TRUE FALSE TRUE
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
TRUE
and FALSE
T
is the short of TRUE
F
is the short of FALSE
So these two commands give the same result
[1] TRUE TRUE FALSE TRUE
[1] TRUE TRUE FALSE TRUE
T
and F
are for lazy peopleUsing T
instead of TRUE
saves some time
But it is not so clear when you read it
Moreover, someone may assign a value to T
(it may be temperature or time)
Then things do not work
Better use TRUE
and FALSE
The most important way to make a logic vector is to compare a numeric vector against a fixed value
a <- c(3, 10, 2, 8, 6, 9, 1, 7, 5, 4)
[1] 3 10 2 8 6 9 1 7 5 4
[1] TRUE FALSE TRUE FALSE FALSE FALSE TRUE FALSE TRUE TRUE
Compare a
to a fixed value, and store the result
[1] TRUE FALSE TRUE FALSE FALSE FALSE TRUE FALSE TRUE TRUE
The comparison is done first
The assignment is done later
Vector x
is a logic vector
If two vectors have the same size, we can compare them
[1] 3 10 2 8 6 9 1 7 5 4
[1] 10 5 3 8 1 4 6 9 7 2
[1] TRUE FALSE TRUE TRUE FALSE FALSE TRUE TRUE TRUE FALSE
Copares element-to-element
Operations with logic vectors
These are all combinations of TRUE
and FALSE
[1] TRUE TRUE FALSE FALSE
[1] TRUE FALSE TRUE FALSE
x & y
The result is TRUE
when both values are TRUE
[1] TRUE TRUE FALSE FALSE
[1] TRUE FALSE TRUE FALSE
[1] TRUE FALSE FALSE FALSE
x | y
The result is TRUE
when any value is TRUE
[1] TRUE TRUE FALSE FALSE
[1] TRUE FALSE TRUE FALSE
[1] TRUE TRUE TRUE FALSE
!x
The result is TRUE
when the element is FALSE
[1] TRUE TRUE FALSE FALSE
[1] FALSE FALSE TRUE TRUE
Use parenthesis to give priority to one operation
[1] FALSE TRUE TRUE TRUE
[1] FALSE FALSE TRUE FALSE
[1] FALSE FALSE FALSE TRUE
They are all different
[1] TRUE TRUE FALSE FALSE
any(x)
is TRUE
if any element of x
is TRUE
[1] TRUE
all(x)
is TRUE
if all element of x
are TRUE
[1] FALSE
[1] TRUE TRUE FALSE FALSE
length(x)
is the number of elements in x
(size)
[1] 4
sum(x)
counts the number of x
elements that are TRUE
[1] 2
that is, text
Text is a string of characters (letters, symbols)
Each text must be between single or double quotes
[1] "alpha"
[1] "beta"
You can use either '
or "
, but you have to be coherent
Use single quotes inside double quotes, and vice-versa
[1] "I don't know"
[1] "he said \"yes\""
Notice that "
inside "
is written as \"
Some special characters are coded with two symbols:
\"
, \\
, \n
, \t
They may look similar, but they are different
"TRUE"
is not TRUE
"123"
is not 123
Quotation marks always indicate text
No quotation marks, no text
When should you use numbers?
0
Store age
and weight
as numbers
Store student_number
as text
Same idea as numbers and logic
Concatenation (using c()
) create a vector
[1] "Ali" "Deniz" "Fatma" "Emre" "Volkan" "Onur"
[1] "Ali" "Deniz" "Fatma" "Emre" "Volkan" "Onur"
length(x)
is the number of elements in x
(size)
[1] 6
nchar(x)
is the number of characters in each element
[1] 3 5 5 4 6 4
substr()
gives part of each element
[1] "Ali" "Den" "Fat" "Emr" "Vol" "Onu"
We must tell which is the first and the last position of each substring
[1] "A" "D" "F" "E" "V" "O"
We can paste two or more vectors and get a new one
[1] "Ali A" "Deniz D" "Fatma F" "Emre E" "Volkan V" "Onur O"
Useful to combine given name and family name.
We can also collapse all elements into a single text
[1] "Ali, Deniz, Fatma, Emre, Volkan, Onur"