Codecademy Logo

Learn R: Introduction

R Logical Data Type

The R logical data type has two possible values - TRUE or FALSE.

It is important for the capitalization to stay as shown and to make sure not to wrap the values in quotes.

NA Data Type in R

R conveys an absence of value with the keyword NA (no quotes). There is a numeric NA, a character NA, and a logical NA. Each means a non value within its context.

Mathematical Operations in R

In R, the conventional symbols +, -, *, and / are used for addition, subtraction, multiplication, and division. The PEMDAS order of operations governs these mathematical operations in R.

# Results in "500"
573 - 74 + 1
# Results in "50"
25 * 2
# Results in "2"
10 / 5

R Data Types

In R, and in programming in general, data types are the classifications that we give to different kinds of information pieces.

Specifically, R provides the following basic data types: character, numeric, integer, logical, and complex. Each data type is used to represent some type of info - numbers, strings, boolean values, etc.

# var1 has the number data type
var1 <- 3
# var2 has the character data type
var2 <- "happiness"
# var3 has the logical data type
var3 <- TRUE

R Conditional Statements

In R, a conditional statement goes inside parentheses and is followed by a set of curly braces that contain the code to be executed.

In an if statement, if the conditional is True then the code inside the curly braces is executed.

if (n == 5) {
print('n is equal to five')
}

R Comments

R interprets anything on a line after a # symbol as a comment.

Comments can be used to add text in the program that will NOT be executed. They are useful for providing context, hints for yourself or others working on the code, or even to temporarily get rid of a line when debugging.

# This is a comment!

R’s Character Data Type

R’s character data type includes all string values. A character is denoted by its surrounding quotation marks. Characters are any text or grouping of keyboard characters, including letters, numbers, spaces, symbols, etc. The character version of a number is the text conveying a number. The number itself, without quotes, is numeric.

Assignment Operator in R

In R, there are two ways to assign values to variables. We can use the assignment operator, an arrow sign (<-) made with a carat and a dash. It is also acceptable, though less preferred, to use an equal sign (=).

R Numeric Data Type

The numeric data type in R is a class that represents numbers that can be integers or decimals.

example <- 4
another.example <- 15.2

Learn More on Codecademy