Aug 11, 2022
Data Types
In R
and in programming, data types are the classifications we give different kinds of information pieces.
In this lesson, we will explore the following R data types:
Numeric
: Any number with or without a decimal point: 23, 0.03 and the numeric null value NA.
class(2) # numeric
Character
: Any grouping of characters on your keyboard (letters, numbers, spaces,
symbols, etc.) or text. Most strings are surrounded by single quotes: ‘ … ‘ or double
quotes “ … “, though we prefer single quotes. Sometimes you will hear this type referred to as “string.”
class('hello world') # character
Logical
: This data type only has two possible values— either TRUE or FALSE (without
quotes). It’s helpful to think of logical types or booleans as on and off switches or as the answers to a “yes” or “no” question.
class (FALSE) #logical
Vectors
: A list of related data that is all the same type.
NA
: This data type represents the absence of a value, and is represented by the keyword
class(NA) #logical
NA (without quotes) but it has its own significance in the context of the different types. That is there is a numeric NA, a character NA, and a logical NA.
Variables
In programming, variables allow us to store information and associate that
information with a name. In R, we assign variables by using the assignment operator,
an arrow sign (<-) made with a carat and a dash.
example_variable <-"Hello world!"
print(example_variable)
Vectors
In R, vectors are a list-like structure that contain items of the same data type.
Take a look here:
spring_months <- c("March", "April","May","June")
In the example above, we created a new variable with the value of a new vector.
We created this vector by separating four character strings with a comma and wrapping them inside c().
A few things you should know how to do with vectors:
- check the type of elements in a vector -
typeof(vector_name)
- check the length of a vector -
length(vector_name)
- You can access individual elements in the vector by using [] and placing the element position inside the brackets. For example, if we wanted to access the second element we would write: vector_name[2].
In R
, you start counting elements at position one, not zero.
Conditionals
In R, we will often perform a task based on a condition. For example, if we are analyzing data for the summer, then we will only want to look at data that falls in June, July, and August.
We can perform a task based on a condition using an if statement:
if (TRUE) {
print('This message will print!')
} else {
print("This message don't will print!")
}
Notice in the example above, we have an if statement. The if statement is composed of:
- The if keyword followed by a set of parentheses () which is followed by a code block, or block statement, indicated by a set of curly braces {}.
- Inside the parentheses (), a condition is provided that evaluates to TRUE or FALSE.
- If the condition evaluates to true, the code inside the curly braces {} runs, or executes.
- If the condition evaluates to false, the code inside the block won’t execute.
Knowing how to use if statements will help you introduce logic in your data analysis.
There is also a way to add an else statement. An else statement must be paired with an if statement,
and together they are referred to as an if…else statement.
Comparison Operators
When writing conditional statements, sometimes we need to use different types of operators to compare values.
These operators are called comparison operators.
Here is a list of some handy comparison operators and their syntax:
- Less than:
<
- Greater than:
>
- Less than or equal to:
<=
- Greater than or equal to:
>=
- Is equal to:
==
- Is NOT equal to:
!=
Comparison operators compare the value on the left with the value on the right. For instance:
10 < 12 # Evaluates to TRUE
It can be helpful to think of comparison statements as questions.
When the answer is “yes”, the statement evaluates to true, and when the answer is “no”,
the statement evaluates to false. The code above would be asking: is 10 less than 12? Yes!
So 10 < 12 evaluates to true.
Logical Operators
Working with conditionals means that we will be using logical, true
or false
values.
In R
, there are operators that work with logical values known as logical operators.
We can use logical operators to add more sophisticated logic to our conditionals.
There are three logical operators:
- the AND operator (
&
) - the OR operator (
|
) - the NOT operator, otherwise known as the bang operator (
!
)
When we use the &
operator, we are checking that two things are true:
if (stopLight == 'green' & pedestrians == 0) {
print('Go!');
} else {
print('Stop');
}
When using the &
operator, both conditions must evaluate to true for the entire condition to evaluate to true and execute.
Otherwise, if either condition is false, the &
condition will evaluate to false and the else block will execute.
If we only care about either condition being true, we can use the |
operator:
if (day == 'Saturday' | day == 'Sunday') {
print('Enjoy the weekend!')
} else {
print('Do some work.')
}
When using the |
operator, only one of the conditions must evaluate to true for the overall statement to evaluate to true.
In the code example above, if either day ==
‘Saturday’ or day ==
‘Sunday’ evaluates to true the if’s condition will evaluate to true and its code block will execute.
If the first condition in an |
statement evaluates to true, the second condition won’t even be checked.
The !
NOT operator reverses, or negates, the value of a TRUE value:
excited <- TRUE
print(!excited) # Prints FALSE